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
2 Answers
2
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
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
so you have a MouseLook script attached to the SAME object as this script (presumably the character)? AND one on the camera?
– Seth-BergmanNot to the camera, but thats where the Camera.main.GetComponent(MouseLook) comes in. And the mouselook script itself is indeed attached to the same object
– LesPaulI was asking about the script called MouseLook 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?
– Seth-Bergmanok you already answered my first question below..
– Seth-Bergmanyou might just try deleting the line and re-type it by hand, sometimes weird ansi chars mess up these lines, even though it LOOKS identical..
– Seth-Bergman