- Home /
Change global variable on input first than following scripts
I am making a game in which the player have the ability to switch back and forth between two different states. This happens when the player press one key.
So basically I´m using a variable, lets say, state
which toggle between true and false on every input.
To avoid listening to the input on each script I have an empty gameObject with a script attached, listening for the user input, and subsequently changing the value of a global variable.
From the other srcipts in the game, I am accesing this variable clling the name of the script following by the variable, ex. scriptName.state
.
The problem is that when I check this global value on the other scripts, depending on the order of execution the value is going to be changed or not, leading to unacceptable behaviors.
I know there is a way to alter the order of execution, but I don´t know if this is performance wise or not.
What are your thoughts on this matter? So I have to manage to execute the global script at first? Or there is another technic better than this?
Thanks!
I would have the key listener method on the other object set a var to be true/false on the player objects. The player states scripts would be running with a 'if true' condition around the code. Doing it this way will also allow you to keep better track of instantiating player prefabs and allows for more states to be added easily, if you should choose to do so.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Having a script variable update between two objects 1 Answer
Script References Exploration 0 Answers
How to access variables from a different script on a different GameObject? 2 Answers
Changing a variable to the next one in order automatically.. 1 Answer