Panel colour changes?

Hi guys,

I’m following a Tower Defense tutorial and I am trying to code colour changes in the panels so if a player has enough money to purchase the turret the panel is green, and if they don’t it’s red! I’ve managed to get it work except using one script to manage all the panels but it makes the highest cost apply to all of them. So if I purchase 1 turret (panel, £100) both panels will go red, even though I’ve still got £200 (enough for two standard turrets) – I don’t really want to have to create a script for each panel because it will clog up my folders. Anyway of managing this in one script without getting the problem I have?

(All the stupid names like panel, panel2, are just place holders to be updated later on.)

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;
public class UICost : MonoBehaviour {

private BuildManager buildManager;

private object panelColor;

private object panelColor2;

private Renderer rend;

Image panel;

Image panel2;
public bool standardTurretHasMoney { get { return PlayerStats.Money >= 100f; } }

public bool Panel2HasMoney { get { return PlayerStats.Money >= 250f; } }

// Use this for initialization

void Start () {

panel = GetComponent<Image>();

panel2 = GetComponent<Image>();

}
// Update is called once per frame

void Update()

{
if (standardTurretHasMoney)

{

panel.color = UnityEngine.Color.green;

}

else

{

panel.color = UnityEngine.Color.red;

}

if (Panel2HasMoney)

{

panel2.color = UnityEngine.Color.green;

}

else

{

panel2.color = UnityEngine.Color.red;

}

}

}
internal class panel

{

internal Color color;

}

I’m not sure I fully understand the problem but it sounds like you want to move away from hard coded values.
E.G use a variable for the cost, you can then change this value for each turret that has the script.

public class UICost : MonoBehaviour
{
    public float cost = 100; // You can change this value per script instance
   
    public bool standardTurretHasMoney { get { return PlayerStats.Money >= cost; } }
}

I’m okay with the costs, they’re coded elsewhere, this is specifically to recolour the panels depending on whether or not the player has enough money to purchase the associated turrets. E.g.
Turret 1 = £100
Turret 2 = £200
Player Money = £150 so, turret 1 should be green, and turret 2 should be red.
I eventually just created a single script for each turret, but it would be easier if I could code it all in one script.