I ended up pausing right at the beginning and did everything by myself and it worked exactly as intended!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefenderSpawner : MonoBehaviour
{
Defender defenderPrefab;
int defenderPrice;
int rupeeWallet;
//Delcared vars??
// Vector2 worldPosition;
//Debug controls
//make these in their own catagory called dogeDebugParams
//I CANT REMEMBERRRR
[SerializeField] bool dogeIsPermaPos;
[SerializeField] Vector2 dogePermaPosition;
// Start is called before the first frame update
// Update is called once per frame
void OnMouseDown()
{
//MousePositioner();
SpawnDefender(MousePositioner());
rupeeWallet = FindObjectOfType<RupeeCounter>().rupees;
}
public void SetSelectedDefender(Defender defenderToSelect, int priceOfRupees)
{
defenderPrefab = defenderToSelect;
defenderPrice = priceOfRupees;
}
Vector2 MousePositioner()
{
Vector2 mousePos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
Vector2 worldPosition = Camera.main.ScreenToWorldPoint(mousePos);
Vector2 gridPos = SnapToGrid(worldPosition);
return gridPos;
//worldPosition = Camera.main.ScreenToWorldPoint(mousePos);
}
private Vector2 SnapToGrid (Vector2 rawWorldPos)
{
float newX = Mathf.RoundToInt(rawWorldPos.x);
float newY = Mathf.RoundToInt(rawWorldPos.y);
return new Vector2(newX, newY);
}
void SpawnDefender(Vector2 worldPosition)
{
//GameObject newDefender = Instantiate (defenderPrefab, worldPosition, Quaternion.identity) as GameObject;
if (rupeeWallet >= defenderPrice)
{
Defender newDefender = Instantiate (defenderPrefab, worldPosition, Quaternion.identity) as Defender;
FindObjectOfType<RupeeCounter>().SpendRupees(defenderPrice);
Debug.Log("Defender Placed");
// Instantiate(defenderPrefab, worldPosition, Quaternion.identity);
}
else if(defenderPrice >= rupeeWallet)
{
Debug.Log("NotEnoughRupees");
}
}
}
But when I saw Rick do it I see that he has cached a reference to the counter itself and made a return bool method that tells it whether or not the cost enough. I did sort of the same thing but more so on the fly and I had DefenderSpawner.cs do most of the work.
Is this worse or less efficient? should I change it to match Ricks or will I be fine moving on?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefenderButton : MonoBehaviour
{
[SerializeField] Defender defenderObject;
[SerializeField] int starValue = 20;
// Start is called before the first frame update
private void OnMouseDown()
{
var buttons = FindObjectsOfType<DefenderButton>();
foreach(DefenderButton button in buttons)
{
button.GetComponent<SpriteRenderer>().color = new Color32(41,41,41,255);
}
GetComponent<SpriteRenderer>().color = Color.white;
FindObjectOfType<DefenderSpawner>().SetSelectedDefender(defenderObject, starValue);
}
// Update is called once per frame
void Update()
{
}
}
this is DefenderButton.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RupeeCounter : MonoBehaviour
{
[SerializeField] public int rupees = 60;
Text rupeeText;
private void Start()
{
rupeeText = GetComponent<Text>();
UpdateDisplay();
}
private void Update()
{
}
private void UpdateDisplay()
{
rupeeText.text = rupees.ToString();
}
public void AddStars(int amount)
{
rupees += amount;
UpdateDisplay();
}
public void SpendRupees(int amount)
{
if (rupees < amount)
{
rupees = 0;
}
else
{
rupees -= amount;
UpdateDisplay();
}
}
}
And this is my version of Starcounter known as RupeeCounter.cs