I managed to get the units to only appear once the player has enough energy to summon them, using the following script:
public class EnergyCheck : MonoBehaviour
{
public StarDisplay energyTotal;
[SerializeField] int defenderCost;
public void UpdateButtonColour()
{
if (energyTotal.stars < defenderCost)
{
GetComponent<SpriteRenderer>().color = new Color32(41, 41, 41, 255);
}
else
{
GetComponent<SpriteRenderer>().color = Color.white;
}
}
private void Update()
{
UpdateButtonColour();
}
}
This script is added to each defender unit.
Need to drag the Star Text from the canvas into the Energy Total field.
Need to enter the energy cost of each unit into the Defender Cost field.
Need to adjust Rick’s StarDisplay script by changing the line from:
[SerializeField] private int stars = 100;
to:
[SerializeField] public int stars = 100;
Also keep Rick’s Defender Button script, but remove the lines that change unit transparency. So what is kept is:
public class DefenderButton : MonoBehaviour
{
[SerializeField] Defender defenderPrefab;
private void OnMouseDown()
{
var buttons = FindObjectsOfType<DefenderButton>();
foreach(DefenderButton button in buttons)
{
FindObjectOfType<DefenderSpawner>().SetSelectedDefender(defenderPrefab);
}
This will make defender units visible only when enough energy is available to summon them. It continues to select the unit when you click on them but no longer makes them visibly selected by doing so.
What is needed now is to create a selection box when units are clicked on - so you know which unit is selected.
I’m close to figuring this out, but I can’t quite get it to work properly.
What I have done so far, is created a game object with a sprite renderer component (I guess that’s a sprite? lol), and attached a sprite image of a box outline to it. I created one for each location on the quad, so there is a selection box positioned for each unit. I then deactivated each of them so they start disabled.
I then created a script called EnableSquare.cs:
{
public GameObject square;
private void OnMouseDown()
{
square.SetActive(true);
}
I attached this script to each of the defender units in the quad, and dragged each unit’s respective selection box sprite to the Square field.
This works when you click on a unit - the selection box appears on the unit and the script that makes the units visible based on energy continue to work.
However, as you can tell from the code - once you click on the unit the graphic of the selection box stays there forever.
I am trying to figure out how to get the selection box to remove when a new unit is clicked on.
I have checked the Unity API, and tried the isActiveAndEnabled property, but cannot seem to get it to work. Was wondering if there are any other options I could check out.