Search code examples
c#unity-game-engine

Unable to access a the variable on another script


I have this class attached to a PauseButton object.

public class PauseButton : MonoBehaviour
    {
        public bool firstTimePause;
        private bool checkIfPause;
        public void Awake()
        {
            firstTimePause = false;
        }
        public virtual void PauseButtonAction()
        {
            StartCoroutine(PauseButtonCo());
        }   
        protected virtual IEnumerator PauseButtonCo()
        {
            yield return null;
            checkIfPause = GameObject.Find("SomeObject").GetComponent<GameManager>().Paused;
            if (firstTimePause == false && this.gameObject.name == "ResumeBackground" && checkIfPause == true)
            {
                firstTimePause = true;
                Debug.Log("This is getting printed");
            }
        }
    }

Then I have another class trying to access the firstTimePause variable on PauseButton

public class StopWatchTimer : MonoBehaviour
{
    public Text textTime;

    private PauseButton pauseButtonScript;
    public GameObject pauseButtonObject;

    // Use this for initialization
    void Start()
    {
        pauseButtonScript = pauseButtonObject.GetComponent<PauseButton>();
    }
    void Update()
    {
        pauseButtonScript = pauseButtonObject.GetComponent<PauseButton>();
        Debug.Log(pauseButtonScript.firstTimePause); //this value is always false even if it was already set to true on PauseButton Class
        if (pauseButtonScript.firstTimePause == true)
        {
           //do something
        }
    }
}

Why is it that I always gets False on the variable firstTimePause even though I checked that it was set to True by having a Debug.Log

If I changed the class into like this, it is working.

public class PauseButton : MonoBehaviour
    {
        public bool firstTimePause;
        private bool checkIfPause;
        public void Awake()
        {
            firstTimePause = false;
        }
        public virtual void PauseButtonAction()
        {
            StartCoroutine(PauseButtonCo());
        }   
        protected virtual IEnumerator PauseButtonCo()
        {
            yield return null;
            checkIfPause = GameObject.Find("SomeObject").GetComponent<GameManager>().Paused;
                firstTimePause = true;
                Debug.Log("This is getting printed");
        }
    }

This means there is something wrong on the statement this.gameObject.name == "ResumeBackground" && checkIfPause == true. But since both scenarios are printing "This is getting printed", I'm getting confused why it won't work as my expectation.

This is SomeObject with GameManager enter image description here


Solution

  • I finally figured it out. Apparently, I have 2 objects that the script is attached.

    The first object is where I am setting the variable to become true/false. And checking via Debug.Log shows that it is correctly setting the true/false.

    The second object is where I am checking if true/false which always false since it was not being changed. I wanted to thank @Erik Overflow which made me think about this.