Assign a variable on a class where other classes derive from with Awake?

I want to reference another class/script:

public class SavableClass extends MonoBehaviour
{  
	public var cSaveManager : SaveManager;
	 
    public function Awake() : void
    {
    	cSaveManager = FindObjectOfType ( SaveManager );
		Debug.Log( "Executed");
    }
} 

This script is only used trough its derived classes.

The problem is that he Awake function does not seem to work. (I do not get the ‘executed’ log message )
Replacing Awake with Start seems to work, are there other solutions to this as well?

Using a constructor does not seme to work because FindObjectsOfType will not work there.

There doesn’t seem to be anything wrong with your Awake method, but if it’s not being called while Start is, it could be because another Awake is defined in your deriving classes (and no Start is defined in them). In object oriented programming in .Net, methods are always called in order of bottom-to-top in the inheritance hierarchy. So, when Unity calls Awake on all scripts, the runtime will search the most derived classes for a definition first. If it finds one, that one gets called instead of whatever Awake might also be defined in that script’s parent class. This is called “hiding” the method in the base class, and the C# compiler issues a warning about that if the child version is not declared with the “new” modifier. I’m not sure the UnityScript compiler does so, too.

If this is the case, you can let the call to Awake traverse up the inheritance tree by making your Awake virtual in the parent class, then adding “override” to the ones in the children, and calling “base.Awake” in the child versions of Awake. If my theory is wrong, then something else is going on and you can safely disregard all of the above. :wink:

On a finishing note, you must never declare constructors for classes that derive from MonoBehaviour, because Unity’s serialization engine can call the constructors of all scripts in editor time at any time and cause unexpected behaviour for you.