Probably Dumb Question: Update(), Start(), etc. Don't Work Inside Classes (JavaScript)...?

I fully expect that this is simply because I’ve never tried to put Update() or Start() inside of one of my own classes before. There is probably some simple thing I’m missing. I have, however, worked fairly extensively with classes for a while now - I’ve just never tried to do this.

I have the following code:

#pragma strict
 
public class test1 {
     public function test1() {
          // Stuff
     }

     public function Start() {
          // Stuff, including a Debug.Log()
     }
     
     public function Update() {
          // Stuff, including a Debug.Log()
     }
}


public static class test2 {

     public function Start() {
          // Stuff, including a Debug.Log()
     }
     
     public function Update() {
          // Stuff, including a Debug.Log()
     }
}

function Start() {
     new test1();
}

I have placed the script on a GameObject in the scene. None of the Debug.Log()s from the Start()s and Update()s are showing up. To my understanding, both test1 and test2 inherit from MonoBehaviour, which means they should automatically run these types of functions. But am I wrong? Do I really have to call them from a script’s Update, Start, Awake, etc. functions manually?

I tried to find this question on the internet, because I’m sure it’s common, but Google doesn’t like me today. Feel free to link if you can find an existing answer.

PS: This is probably a related issue in my understanding. If I try to have a script that only has #pragma strict followed by a single class (no functions or variables outside the class), Unity won’t recognize the script as a resource. Is that normal? This is Unity 4.6.x.

Whats that random extra Start at the end?

It shouldnt be there. Also, your classes dont derive from MonoBehaviour as you dont implement/extend it. This is why your Mono functions are not running.

public class test1 extends MonoBehaviour
{

}

Well, if you need non MonoBehaviour classes (not attached to any gameobject) you have to call any methods in those classes yourself. Unity’s won’t automatically call any method of ordinary classes. Unity itself (the engine core) doesn’t even know those class instances exist and doesn’t really care about what classes you create on your own.

Classnames should always start with a capital letter. Here’s an example in UnityScript:

public class Test1
{
    public function Test1() {
        Debug.Log("Test1::constructor");
    }

    public function Start() {
        Debug.Log("Test1::Start")
    }
      
    public function Update() {
        Debug.Log("Test1:Update");
    }
}

var instances : Test1[];

function Start() {
    instances = new Test1[3]; // array with 3 elements
    for(var i = 0; i < instances.Length; i++)
    {
        instances *= new Test1();  // create 3 instances and store them in the array*

instances*.Start(); // call the Start method of each the new instance*
}
}

function Update() {
for(var i = 0; i < instances.Length; i++)
{
// each Update of the MonoBehaviour we call the Update of our 3 instances
instances*.Update();*
}
}
When you create a “normal” class instance you have to store the reference that is returned by the constructor somewhere. If you don’t you can’t interact with your class. Furthermore Mono uses a garbage collector. So once there’s no reference to an instance the object is automatically removed.
Note: Unity classes that are derived from UnityEngine.Object (like MonoBehaviour and others) have a native code counter part in the engine code (which is written in C++). Those instances are managed by the engine itself and you can use methods like FindObjectOfType to “regain” access to an instance since Unity internally manages those instances.
“Normal” classes are just plain .Net / Mono classes. They don’t do anything on their own. Unity and the Unity editor can only work with classes derived from MonoBehaviour. Those are actual “assets” in the Unity sense. There’s no problem putting your Test1 class into it’s own file called “Test1.js”. The class will still be available to your other classes. However as i said Unity can’t do anything on it’s own with them. The usage is completely up to you.