Home > Software design >  Unable to access a the variable on another script
Unable to access a the variable on another script

Time:06-10

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

CodePudding user response:

firstTimePause == false && this.gameObject.name == "ResumeBackground" && checkIfPause == true
{
...
    Debug.Log("This is getting printed");
}

But since both scenarios are printing "This is getting printed"

Why is it that I always gets False on the variable firstTimePause

This is exactly what you would expect, no? If firstTimePause is always false, then you expect the condition above to always be false.

CodePudding user response:

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.

  • Related