Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LesPaul · Apr 18, 2013 at 06:11 PM · charactercontrollernullreferenceexception

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
Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · Apr 18, 2013 at 07:46 PM 0
Share

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?

avatar image LesPaul · Apr 18, 2013 at 07:49 PM 0
Share

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

avatar image Seth-Bergman · Apr 18, 2013 at 07:59 PM 0
Share

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?

avatar image Seth-Bergman · Apr 18, 2013 at 08:01 PM 0
Share

ok you already answered my first question below..

avatar image Seth-Bergman · Apr 18, 2013 at 08:04 PM 0
Share

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..

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LesPaul · Apr 18, 2013 at 07:43 PM 0
Share

I did in another scene just to test it, but there is only one instance of this script in the current scene

avatar image Yokimato · Apr 18, 2013 at 07:44 PM 0
Share

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.

avatar image LesPaul · Apr 18, 2013 at 07:46 PM 0
Share

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 ;)

avatar image Yokimato · Apr 18, 2013 at 07:51 PM 0
Share

so...you have 2 mouse look scripts? or is the toggle menu script on the camera as well?

avatar image Yokimato · Apr 18, 2013 at 09:08 PM 1
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LesPaul · Apr 18, 2013 at 08:46 PM 0
Share

That is not the problem. I had this script working just fine a couple of hours ago. Nothing changed, that's the weird part...

avatar image Seth-Bergman · Apr 18, 2013 at 08:48 PM 0
Share

did you try my suggestion? just because it worked once doesn't change the facts..

clearly this is the issue

avatar image LesPaul · Apr 22, 2013 at 05:08 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges