Generic type for script variables.

Hey there,

this is my current code (js):

public var cameraMovementScript : MonoBehaviour;

function Start()
{
   cameraMovementScript = cameraMaster.GetComponent(CameraMovement);
   cameraMovementScript.autoCameraMove = true;
}

But I get the error: ‘autoCameraMove’ is not a member of ‘UnityEngine.MonoBehaviour’.

autoCameraMove IS a variable in CameraMovement though. And if all scripts in javascript automatically derive from MonoBehaviour then what’s the problem? I know that if I changed the type to CameraMovement that would work. But I would like to know how to keep it generic.

Thankyouverymuch!

Romano

Yes. The compiler only knows the type you define after the colon in UnityScript and uses that type to see what variables or methods you can access.

The way to keep things generic is to create your own parent class that has all the functionality you need:

public class MyCamera extends MonoBehaviour{
	var autoCameraMove :boolean = false;

	function FocusCamera(){ Debug.Log("Focusing camera...");}
}

public class AMoreSpecificCamera extends MyCamera{
    var specificMove : boolean;
	function FocusCamera(){Debug.Log("A specific Camera with autoMove "+autoCameraMove);}
}

Now, code such as:

var cam : MyCamera;
cam = GetComponent("AMoreSpecificCamera");
cam.FocusCamera();
//prints "A specific Camera with autoMove false"

but

var cam : MyCamera;
cam = GetComponent("AMoreSpecificCamera");
Debug.Log(cam.specificMove); //error, because specificMove is in  AMoreSpecificCamera, not MyCamera