Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 ThatIndividual · Jul 20, 2015 at 03:58 PM · getcomponentnull reference exceptionmouse look

().enabled=false stopped working

Hi,I upon creating pause/console button of sorts in c#,I used get component to disable & enable mouse look (both vertical[in main camera] & horizontal[in fps controller]). This worked fine for couple of days, but after downloading free asset from asset store(had 1 JS script) now my own script gives me a null reference on those lines(inside update).

         if(CheatMenuOpen==true)
         {
             fpsContrl.GetComponent<MouseLook>().enabled=false;
             mcamera.GetComponent<MouseLook>().enabled=false;
         }
         else
         {
             fpsContrl.GetComponent<MouseLook>().enabled=true;
             mcamera.GetComponent<MouseLook>().enabled=true;
         }

fpsContrl & mcamera are public. I have battled with this null reference exception whole morning and I just dont know how to fix it. Any help is appreciated, thanks in advance :|

Comment
Add comment · Show 1
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 ThatIndividual · Jul 21, 2015 at 07:23 PM 0
Share

When that still worked, it "ticked" them to disabled and back(in inspector) when told to.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessespike · Jul 20, 2015 at 04:54 PM

GetComponent needs a type to be specified. Presuming the components are already being referenced in the script, and fpsContrl is of type "FirstPersonController" and mcamera being a "Camera" type:

 if (CheatMenuOpen == true) {
      if (fpsContrl!= null) fpsContrl.enabled = false;
      if (mcamera != null) mcamera.enabled = false;
 } else {
      if (fpsContrl!= null) fpsContrl.enabled = true;
      if (mcamera != null) mcamera.enabled = true;
 }

If you want to get a component from a GameObject:

 fpsContrl = (FirstPersonController) someGameObject.GetComponent<FirstPersonController>();
 mcamera = (Camera) someGameObject.GetComponent<Camera>();


http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

Comment
Add comment · Show 2 · 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 ThatIndividual · Jul 20, 2015 at 05:49 PM 0
Share

O god I apologize, code I show here is missing $$anonymous$$ouseLook all together, first post updated. I referred to the game objects with just setting them public & drag&dropping them to the inspector slots.

avatar image DiegoSLTS · Jul 21, 2015 at 06:43 PM 0
Share

If you use the generic version of GetComponent you don't need the cast, the method already returns an object of the correct type.

  fpsContrl = someGameObject.GetComponent<FirstPersonController>();
  mcamera = someGameObject.GetComponent<Camera>();
avatar image
0

Answer by nisovin · Jul 20, 2015 at 08:13 PM

If you're getting a null reference in that block of code, then one of the following must be true:

  1. fpsContrl is null (not initialized or not set in the inspector)

  2. mcamera is null (not initialized or not set in the inspector)

  3. fpsContrl doesn't have a MouseLook component

  4. mcamera doesn't have a MouseLook component

I'd add some Debug.Log lines to figure out which thing is null. Perhaps something like:

 Debug.Log(fpsContrl);
 Debug.Log(mcamera);
 Debug.Log(fpsContrl.GetComponent<MouseLook>());
 Debug.Log(mcamera.GetComponent<MouseLook>());
Comment
Add comment · Show 5 · 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 ThatIndividual · Jul 21, 2015 at 10:20 AM 0
Share

Debug logs helped all right, seems that only $$anonymous$$ouseLook components return null, although the scripts are on fpsConrt & mcamera.

alt text

lkjhyg.png (61.6 kB)
avatar image nisovin · Jul 21, 2015 at 06:36 PM 0
Share

Hmm... it's hard to say. Are they beco$$anonymous$$g disabled or removed at runtime?

avatar image ThatIndividual · Jul 21, 2015 at 07:02 PM 0
Share

When it worked, it "ticked" to disabled and back(in inspector) when needed.

avatar image nisovin · Jul 21, 2015 at 07:17 PM 0
Share

I mean the $$anonymous$$ouseLook script itself. Is it getting removed from the objects somehow?

avatar image ThatIndividual · Jul 21, 2015 at 07:20 PM 0
Share

No, $$anonymous$$ouse look script is "ticked"/disabled, but it still stayed in the fpsContrl & the camera. Tho now I cant see it due to the Null(=does nothing).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Getcomponent null reference exception 1 Answer

groundcheck function returns a NullReferenceException error when the player is airborne 1 Answer

GetComponent null reference C# 1 Answer

Getcomponent returns null on ios 1 Answer

Null Reference Exception Help 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