UGUI button/script woes

I’m having a bit of trouble with this one, considering I got it to work ONE time using the exact C# script I’ve written here.

I’m really lost here, and I’m revisiting beginning C# after a long hiatus.
I have two buttons in my scene on a canvas, in a panel. I am using an empty object in my scene named “ButtonMaster” with a script named “ButtonScript” attached to it.
I have a separate script in my scene named “Bools” attached to an empty gameobject named and tagged as “GameMaster”.
I’m using the two buttons to toggle the boolean values on the GameMaster object.
My On Click() in the button inspector has the “ButtonMaster” object assigned, and then the ButtonScript.ClickedBuryUnbury assigned on button one and ButtonScript.ClickedKillRezz on the second button respectably.
I’ve gotten these buttons to properly toggle the boolean values within the GameMaster object on and off ONE time. (super weird)
Anyhow, here is the script:

using UnityEngine;
using System.Collections;

public class ButtonScript : MonoBehaviour {

    private GameObject gameMaster;
    private bool killRezzToggle;
    private bool buryUnburyToggle;
    
    void Start () {
        gameMaster = GameObject.FindGameObjectWithTag ("GameMaster");
        killRezzToggle = gameMaster.GetComponent<Bools>().amIDead;
        buryUnburyToggle = gameMaster.GetComponent<Bools>().buriedInGround;
    }
       
    public void ClickedKillRezz()
    {
        killRezzToggle=!killRezzToggle;
    }

    public void ClickedBuryUnbury()
    {
        buryUnburyToggle=!buryUnburyToggle;
    }
}

Can anyone tell me whats going on? Perhaps I’m just coding the toggle incorrectly, but why would it have worked on one occasion? Thanks everyone!

You’re just changing the private bools in the ButtonScript.

It won’t change the Bools script unless you assign them back like:

killRezzToggle = !killRezzToggle;
gameMaster.GetComponent<Bools>().amIDead = killRezzToggle;

I would just get the Bools component in Start and just set it directly.

private Bools _bools;

void Start ()
{
    _bools = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<Bools>();
}

void ToggleKill()
{
    _bools.amIDead = !_bools.amIDead;
}

Or just move the functions into the Bools script, and point the button OnClick to there.

1 Like

Stardog, you are awesome. The reason I’m not doing it in the bools script itself (I know how to get it done there) is that I’m trying and typing all different types of code, and specifically trying to get the hang of the accessing other gameobjects variables and manipulate them. Very much a beginner. I’m still wondering why it fluked and that script ran once without a hitch… Weird.