Difficulties with class inheritance

Hello. I’m having an odd problem with inheriting classes. I have a base class:

class Controller extends MonoBehaviour

Which in it’s awake function, gets a component called ‘UnitClass’ also attached to the gameObject it is attached to:

	protected var unitClass : UnitClass;

	function Awake ()
	{	
		unitClass = GetComponent(UnitClass);

Now. This script isn’t actually what gets added as a component to gameObjects. Instead, I have a set of other scripts, for different kinds of controller (ie one controls player input, all the others are different AIs), all of which inherit from Controller, and all of which pass messages to the UnitClass script via the unitClass variable:

class AIController extends Controller
class PlayerController extends Controller

and so forth.

The PlayerController script works fine:

class PlayerController extends Controller
{
	private var floorLayerMask = 1 << 8; //this is the 'Floor' layer
	
	function TakenDamage (damageInfo : Array)
	{
	}

	function Update ()
	{
		//cast ray from screen to mouse co-ords
		var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;	
		if (Physics.Raycast(ray, hit, Mathf.Infinity, floorLayerMask)) //only collides with Floor layer
		{
			unitClass.WantPosition(hit.point);
		}
		else
		{
			Debug.LogWarning(name + ": Raycast for movement failed");
			return;
		}	
		
		//fire weapons!
		if(unitClass.unitData.weaponSet.length)
		{
			unitClass.unitData.weaponSet[0].wantToFire = Input.GetButton("Fire1") ? true : false; //if mouse 1 is held, return true; if not, return false
		}
	}
}

However, every other script I have extending Controller causes NullReferenceException: Object reference not set to an instance of an object runtime errors, the moment it tries using the unitClass variable. I actually tested it with a coroutine: wait for five seconds, then try passing a command down. Cue hang.

There’s no difference between how the PlayerController script and the other AIController scripts are calling it, but it still causes a crash:

//from the PlayerController class:
unitClass.WantPosition(hit.point);
//from the AIController class:
unitClass.SetTarget(thePlayer.transform, thePlayer.transform.position);

I’m at my wit’s end as to what’s causing this; can anyone help?

Have you tried making it not protected to see if that helps?

No effect taking out protected. I checked using instanceof; the scripts definitely are childs of Controller.

Try calling the base Awake:

public class AIController extends Controller
{
	private function Awake()
	{
		base.Awake();
	}
}

I’m not positive if it’s “base” in JavaScript, it could be “super”, but you get the idea.

Yep, that was it exactly! Thanks a million!

super.Awake(); works perfectly.

I’d been trying things like super unitClass etc and failing miserably…

Erm… double post. Whoops.