Hello,
when I load a scene, i want to hide two buttons, but my code doesn’t work.
private GameObject mExitButton;
private GameObject mResumeButton;
void Start(){
mExitButton = GameObject.FindGameObjectWithTag ("InGameExitButton");
mResumeButton = GameObject.FindGameObjectWithTag ("InGameResumeButton");
mExitButton.SetActive (false);
mResumeButton.SetActive (false);
}
I get no erros when assigning to variables, that means that they are assigned correctly (no null reference).
SetActive works with other game objects, it just doesn’t work with buttons. Is there any other function that should be called on UI elements ? (altough SetActive also works for Canvas elements in my case).
Regards, L
Is this script on an active game object?
Are you sure it’s running? (put a debug.log in there to make sure the script is running)
lukagril:
Hello,
when I load a scene, i want to hide two buttons, but my code doesn’t work.
private GameObject mExitButton;
private GameObject mResumeButton;
void Start(){
mExitButton = GameObject.FindGameObjectWithTag ("InGameExitButton");
mResumeButton = GameObject.FindGameObjectWithTag ("InGameResumeButton");
mExitButton.SetActive (false);
mResumeButton.SetActive (false);
}
I get no erros when assigning to variables, that means that they are assigned correctly (no null reference).
SetActive works with other game objects, it just doesn’t work with buttons. Is there any other function that should be called on UI elements ? (altough SetActive also works for Canvas elements in my case).
Regards, L
The code looks OK. Make sure the objects are tagged correctly.
Yes, code was running and game objects were taged correctly. I already found a different way to solve my task (more complicated), but I still have no clue why that code hasn’t worked.
Regards, L
If anyone wants to know how I solved it:
void Start(){
mExitButton = GameObject.FindGameObjectWithTag ("InGameExitButton");
mResumeButton = GameObject.FindGameObjectWithTag ("InGameResumeButton");
EnableOrDisableButton (mExitButton, false);
EnableOrDisableButton (mResumeButton, false);
}
private void EnableOrDisableButton(GameObject button, bool active){
button.GetComponent<Image>().enabled = active;
button.GetComponent<Button>().enabled = active;
button.transform.GetChild(0).GetComponent<Text>().enabled = active;
}