- Home /
Static variables not working inside Function Update()
Hello ! Well, I have a big problem that I can't solve by any way and all I find on the internet is resolving the problem by the same way and it doesn't work for me.
If you get a static variable with the GetComponent stuff like explained in the Tutorial section of the website you have to put it inside Function Update() (if you don't, Unity calls an error). But it's okay, I went ahead of it. However, if now I want to use that variable inside a Function MonoBehaviour(), Function [any type of collision/trigger] or just somewhere else than inside Function Update(), the system shows an error.
I write some codes line right under to explain it beter.
function Update() {
var totems:Totem = GetComponent("Totem"); // Totem is how my other script is called
}
function OnCollionEnter(){
if(totems.totem1==true){ // totem1 is a static boolean variable
//Do Something
}
}
Answer by goo-muffin · Jan 18, 2014 at 10:46 AM
If you have a static variable then you will not need any instance of this script to call this static variable. lets say you got a non static variable in a script:
Script.js
var anthing :boolean;
_______________________
var sc :Script = GetComponent("Script");
var anythingInThisScript :boolean = sc.anything;
if it is static the only thing you should do is to pack the variable in a class:
Script.js
class Totems
{
static var totem1 :boolean;
}
------------------------
anyScript.js
function Update() //or any other
{
var totem1 :boolean = Totems.totem1; //no instance of this class or script is necessary
}
I hope you understood it:
static variable -> no instance of this class or script. It is "static" and in each instance the same
non-static variable -> can be changed from instance to instance and is "non-static". You need to have an instance of the class/Script to get/change the variable
(Disclaimer, I am no believer and not trying to convert anyone)
Think of static like God, there is only one god for all human. Whether there is a human or not god is there and belong to all human. So you would make god static since it belongs to the human and not to a particular human. And you would access it via the Human class as Human.god ins$$anonymous$$d of via an instance of a human.
Seems to work fine (let's see if it does too when I apply it to my full project). However I get one yellow error "The class defined in the script file named 's1' is not derived from $$anonymous$$onoBehaviour or ScriptableObject!", ¿ what does that mean exactly ?
I used the class system for static variables you explained on my project and it suddenly called tons (like 15) errors about all identifiers I've set up declaring a variable of type GameObject for example and the dragging on the main screen the element I want to. However now it says something like "The associated script cannot be loaded, fix errors first to be able to compile it", so I cannot drag anything. The strange thing is that before changing all the static variables all worked well....
well if it worked well then first thing I would do is to undo all the changes... change it back to how it looked before. Then you can try again with more concentration. Well my script should not be the solution! It was only an explination of the meaning of the word static (more or less).
I know, but in fact, I've done it again and checked 10 times for errors and all seems correct, I don't know why the system don't recognize assignated GameObjects (and don't let me drag them in the main menu to maybe fix the errors......).