Hello!
I am currently having trouble working with UI.
My current problem is this:
I have a timer in each scene (apart from main menu), this is done with a canvas + text object + a script in each scene. I would like to have a toggle-able option to turn off the timer, from the main menu.
How do I set inactive my canvas using a script from the main menu? The scene in which the Timer is active is a different scene to the main menu.
Thank you in advance, kind regards.
Jon
Use a global boolean named (say) timerActive, in the main menu script. You mark it “static”:
public static bool timerActive = true;
Any script in any scene can set this static/global variable like this:
mainMenuScriptName.timerActive = false;
or get it like this:
if (mainMenuScriptName.timerActive == false) {} // deactivate canvas
Calling it “global/static” is not exactly correct, but the idea is there. Static means it’s a class-level variable, so it doesn’t have instances when you attach the script to multiple gameObjects. There’s just one value for it, and it exists everywhere (outside of any gameObject the script might be attached to). A little weird at first, especially because it persists even if the gameObject with the script is destroyed. So that’s why I call it “global” in the context of Unity, that it persists across scenes.
1 Like