Why am I getting this error ?

I have a line of code in my script to access and enable/disable a certain component on a game object…

var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("CharacterMotor");

Works fine.

When I change it to this…

var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("MainCamera");

I get this error message…

NullReferenceException: Object reference not set to an instance of an object

Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)

Any suggestions please guys ???


var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent(Camera);

Since the Camera is not a Component i don’t think you can get your reference to the Camera with GetComponent.
You could either use a Tag and get the Camera with GameObject.FindGameObjectWithTag(“MainCamera”) or you could get it by Name for example:


or even with:


you would have to check the js Versions of the above code since i do only use c# i’ not sure if the Syntax is the same.