- Home /
Null Reference Exception with GetComponent() while component is attached
Hey Guys,
I ran into a weird problem... I'm creating a menu that has to show on a keypress, and when that key is pressed it has to disable any controls on the FPS-controller. Somehow this piece of code doesn't work any longer. Any ideas why? The NullReferenceException is on the line this.GetComponent(MouseLook).enabled = !showMenu;
function toggleMenu(){
//Toggle whether or not the menu can be shown
showMenu = !showMenu;
Home();
Screen.lockCursor = !showMenu;
Screen.showCursor = showMenu;
this.GetComponent(MouseLook).enabled = !showMenu;
this.GetComponent(CharacterMotor).canControl = !showMenu;
Camera.main.GetComponent(MouseLook).enabled = !showMenu;
this.GetComponent(MouseHandler).enabled = !showMenu;
}
Any suggestions would be appreciated!
edit: Overview of gameObjects and attached scripts:
Legend:
> = gameObject. When tabbed forward its a child of the first gameobject 1 tab less than the child.
: = Attached Scripts
> First Person Controller : GUI_Handler (script above), MouseLook,CharacterMotor,MouseHandler
> Graphics
> Main Camera : MouseLook
so you have a $$anonymous$$ouseLook script attached to the SA$$anonymous$$E object as this script (presumably the character)? AND one on the camera?
Not to the camera, but thats where the Camera.main.GetComponent($$anonymous$$ouseLook) comes in. And the mouselook script itself is indeed attached to the same object
I was asking about the script called $$anonymous$$ouseLook to be clear, there is one on the camera and one on the player,correct? if you comment out that line, does the rest work?
you might just try deleting the line and re-type it by hand, sometimes weird ansi chars mess up these lines, even though it LOO$$anonymous$$S identical..
Answer by Yokimato · Apr 18, 2013 at 07:41 PM
Have you moved this script recently to a different game object then the game object with MouseLook on it? or vice versa?
You're getting a null reference because it MouseLook was NOT on the same object and therefore doesn't return anything...so when you attempt to access the enabled
field, it errors.
In either case, the solution is to call GameObject.Find
to first get the game object that has MouseLook, then you can call your GetComponent
on it.
EDIT:
Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type.
Per the documentation: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
I did in another scene just to test it, but there is only one instance of this script in the current scene
It has to be on the same game object not the same scene. Call GameObject.Find
first, and then call GetComponent
on it.
The reason why, is that if you just call GetComponent
without using the other gameobject, is that it looks for that component on the current gameobject.
I've got this script attached to my FPS-controller, and that controller has a mouselook script by default. It is still there when i check in the inspector, but gives me that nullreference. I'm not THAT stupid to overlook that ;)
so...you have 2 mouse look scripts? or is the toggle menu script on the camera as well?
But per the documentation, it tells us that we need to call the get component by name rather than type when going across langs. Please try and change the line:
GetComponent($$anonymous$$ouseLook).enabled
to
GetComponent("$$anonymous$$ouseLook").enabled
to see if we can rule this out.
Answer by Seth-Bergman · Apr 18, 2013 at 08:41 PM
The problem is that you can't have c# and javascript scripts together without understanding the following: The communication between the two languages will only go ONE WAY, so you can have the javascript see the MouseLook, but the mouselook won't be able to see ANY javascript scripts. to ensure that MouseLook is compiled first, you need to put it in a priority folder, such as Plugins
http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html
just create a new folder in your base Assets folder called Plugins, and put the MouseLook script in there
That is not the problem. I had this script working just fine a couple of hours ago. Nothing changed, that's the weird part...
did you try my suggestion? just because it worked once doesn't change the facts..
clearly this is the issue
That wasn't the issue. Apparently i had another script called $$anonymous$$ouseLook somewhere in my project folder. But now the issue is that I've got the error that enabled is not a property of type Component
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
NullReferenceException problem 2 Answers
very weird NullReferenceException error causeing one script to break another 2 Answers
Problems with jumping script 1 Answer
controller.SimpleMove slower when Character go Backward 1 Answer