- Home /
Can a static class have an Update() function which Unity calls automatically?
I have script A, NOT attached to any GameObject:
public static class Trial {
var foo : int = 17;
function Update() {
foo++;
}
}
And script B, attached to a GameObject in the scene:
var bar : int;
function Update() {
Trial.Update();
bar = Trial.foo;
}
This works fine, and allows me to access the static class variable Trial.foo -- which increments every frame. However, why does Unity automatically run the Update() (among others) functions in scripts attached to GameObjects? Is there any way for me to have the above example WITHOUT script B needing the Trial.Update(); line?
Comment
Best Answer
Answer by Jessy · Feb 18, 2011 at 05:48 PM
A script has to derive from MonoBehaviour for it to use Update(). A static class can't derive from MonoBehaviour; you can't attach it to a Game Object because it can't be instantiated.
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html