I’m working on a class. I want to use Update. Update doesn’t get called in instances of the class. What am I doing wrong?
class ClassTest extends MonoBehaviour
{
//////// properties
private var timer : float;
//////// methods
// constructor
function ClassTest()
{
Debug.Log("ClassTest Instantiated");
timer = Time.time;
}
// update
function Update()
{
Debug.Log("ClassTest Update");
if (Time.time >= (timer + 1.0))
{
timer = Time.time;
Debug.Log("Another second... " + Time.time);
}
}
}
Now that I think about it, I should be getting an error about trying to instantiate a class that extends MonoBehaviour using the “new” keyword, but I’m not. I create and instance of the class and I get “ClassTest Instantiated” in the console, no warnings. Weird.
OK, I got it. I was using new ClassTest(); to instantiate. I got the debug line about instantiation that way but never got updates.
I tried using AddComponent("ClassTest") instead of new and in the class instead of a constructor I used Awake and now Update works. Meh. Whatever gets it going is fine with me.
class ClassTest extends MonoBehaviour
{
//////// properties
private var timer : float;
//////// methods
// constructor
function Awake()
{
Debug.Log("ClassTest Instantiated");
timer = Time.time;
}
// update
function Update()
{
Debug.Log("ClassTest Update");
if (Time.time >= (timer + 1.0))
{
timer = Time.time;
Debug.Log("Another second... " + Time.time);
}
}
}