- Home /
NullReferenceException-Error when trying to GetComponent
Hi guys!
I got multiple planes (lakes) in my scene and if my player moves to a lower y-value than the plane-positions, an underwater-look is activated!
Every single lake has a script attached to it and calls a function in the script "Overseer" (which is located on a "manager-object"), whenever the player starts diving... Now the problem is, that when the Overseer-script tries to get the component "CharacterMotor" on the main camera to deactivate it, it doesn't work.
In fact, Unity gives me a NullReferenceException-Error. It seems like my script is not capable to/doesn't find the "CharacterMotor"-script!?
Anyway, here are the lines, on which it gives me the error:
...
var normalmotor : CharacterMotor = FirstPersonController.GetComponent.<CharacterMotor>();
if(normalmotor.enabled == false){
normalmotor.enabled = true;
}
...
I really don't know what I did wrong so I'm grateful for any kind of help!
Thanks
Does the FirstPersonController object have the Character$$anonymous$$otor script on it?
Yes. Actually it has an activated Character$$anonymous$$otor-script and an unactive copy of the "Character$$anonymous$$otor-script", called Character$$anonymous$$otorUnderwater on it.
Answer by roaringdoom · Oct 06, 2014 at 01:17 PM
Thank you all for your answers!
I solved the problem by just adding the CharacterController-, the CharacterMotor- and the FSPInputController-Components/Scripts in the Start Function of my "Overseer" and saving it as a variable:
function Start (){
charactercontroller = FirstPersonController.AddComponent(CharacterController);
charactermotor = FirstPersonController.AddComponent(CharacterMotor);
FirstPersonController.AddComponent(FPSInputController);
}
Like that, I was able to disable and enable the different scripts/components:
...
charactercontroller.enabled = false;
charactermotor.enabled = false;
...
Answer by Slev · Sep 21, 2014 at 05:02 PM
You have a typo. It should be:
var normalmotor : CharacterMotor = FirstPersonController.GetComponent<CharacterMotor>();
You had an extra period between the function and the type directive.
Are you sure? Because then it gives me three new errors... Even if I write
var normalmotor : Character$$anonymous$$otor = FirstPersonController.GetComponent(Character$$anonymous$$otor);
it gives me the same NullReferenceException-Error!
just throwing it out there but FirstPersonController. ... seems wrong you need an instance of the class to use GetComponent ie: var firstPersonController : FirstPersonController = FindObjectOfType; or the like
Thank you for your answer! I already defined the variable "FirstPersonController" as a GameObject earlier in the script. So that shouldn't be a problem, right?
I'm sorry if I wasn't clear enough, but I'm using JavaScript!
Answer by cflowers · Oct 04, 2014 at 10:00 PM
You might know some of the things I am about to say but for others sake I am going to state them. First you need a script called exactly "CharacterMotor" for this is case-sensitive. Then one must put the "CharacterMotor" script on the FirstPersonController object. Then one way to call the script is:
var normalmotor:CharacterMotor = (CharacterMotor)FirstPersonController.GetComponent("CharacterMotor");
I did something similar like this, this is in C#:
public GameObject thisObject;//could be any object I want
Monologue mono1 = (Monologue)thisObject.GetComponent("Monologue");//the object I pick must have this script on it or it will be null