1
\$\begingroup\$

I want from my script to change the color of the button that identify from the Function OnClick. It's possible ?

public float Money;
public GameObject[] upgrade_health_level;
public int[] Player1_heal;

void Start()
{
    Money = 60;
    Player1_heal = new int[6];

}

// Update is called once per frame
void Update()
{


} // END UPDATE


public void upgrade_HEAL() // button OnClick
{

        if (Player1_heal[0] == 0 && Money >= 10)
        {
            Money -= 10;
            Player1_heal[0]++;
            upgrade_health_level[0].transform.localPosition = new Vector3(10, 0, 0);
        }
        else if (Player1_heal[0] == 1 && Money >= 10)
        {
            Money -= 10;
            Player1_heal[0]++;
            upgrade_health_level[0].transform.localPosition = new Vector3(20, 0, 0);
            // Change the button color that use this function
        }

}
\$\endgroup\$
1
\$\begingroup\$

Sure. Two options.

  1. Pass the button as a parameter to the method. You can do this by adding Button b as a parameter, telling the method in the inspector (which is how I assuming its wired up already), and then dragging the button into the new parameter field that shows up.

  2. Doing everything through code instead.

    Button btn = SomeGo.GetComponent<Button>();
    btn.onClick.AddEventListener(delegate () {
        //code here
        //btn is accessible
    })
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.