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 Xyex Development · Mar 24, 2013 at 07:58 PM · guinullreferenceexceptiondisabling

NullReferenceException

I keep getting a NullReferenceException whenever I am trying to disable a script from another script. The exact error is NullReferenceException: Object Reference not set to an instance of an object. I really don't understand what is going on and it's really getting on my nerves, any help would be appreciated. Code is below.

 var information: String;
 
 var script: MouseLookPlus;
 
 var script2: MouseLookPlus;
 
 private var guiOn = false;
 
 private var rect: Rect;
 
 function MouseToggle () {
     
     script = GetComponent(MouseLookPlus);
     script.Enabled = false;
     script2 = GetComponent(MouseLookPlus);
     script2.Enabled = false;
 
 }
 
 function OnMouseDown () {
     guiOn = true;
     rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
     MouseToggle();
 }
 
 
 function OnGUI () {
 
     if (guiOn){
         
         GUI.Label(rect, information);
         
         if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
                 guiOn = false;
         }
         if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
             animation.Play ("dooropen1");
             guiOn = false;
         }
         
     }
     
 }

I have two Script variables because there are two scripts I'm attempting to disable when the GUI comes on.

Comment
Add comment · Show 4
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 robertbu · Mar 24, 2013 at 08:30 PM 1
Share

It would be helpful if edited your quetion and comment the line that is causing the error. Without you showing us, we have to guess. GetComponent() as you've used it here gets a component on the current game object. Are you sure the $$anonymous$$ouseLookPlus script is attached to the same game object as this script?

avatar image gribbly · Mar 24, 2013 at 09:25 PM 0
Share

Agreed, please cut and paste the actual error - it should contain the line number which will make it much easier for us to help you

avatar image Xyex Development · Mar 24, 2013 at 09:31 PM 1
Share

This is the actual error I get in my Debug Log, and no the mouselook scripts are tied to the main camera, and first person controller, while the script that is executing the command is tied to the object which produces the GUI

 NullReferenceException: Object reference not set to an instance of an object
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value)
 openscript1.$$anonymous$$ouseToggle () (at Assets/$$anonymous$$isc Scripts/OpeningScripts/openscript1.js:14)
 openscript1.On$$anonymous$$ouseDown () (at Assets/$$anonymous$$isc Scripts/OpeningScripts/openscript1.js:23)
 UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
avatar image gribbly · Mar 24, 2013 at 09:42 PM 0
Share

Looks like

 script = GetComponent($$anonymous$$ouseLookPlus);

...is failing. I would do:

 if(script == null) Debug.Log("SCRIPT IS NULL!");

...right after line 14 to confirm.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gribbly · Mar 24, 2013 at 09:45 PM

Oh yeah, robertbu got it right. GetComponent will only work like that if MouseLookPlus is attached to the same object as this script.

You need to do something like:

 script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);

...basically get hold of your camera object and call GetComponent on that.

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 Xyex Development · Mar 24, 2013 at 10:02 PM 0
Share

Thank you so much, after days of searching it finally worked! Should make this so much easier now. Thanks so much!!!

avatar image Xyex Development · Mar 25, 2013 at 12:10 AM 0
Share

Unfortunately, I have now reached another problem. See I have multiple objects that create a GUI. So once I try to add the coding to stop the movement when the GUI comes on, none of them except one of the objects work. Basically the way it works is that I have several doors that open pending the answer of their respective questions. But when I apply the camera disabling script to the second door, the first door GUI stops disabling the camera script. Any ideas?

GUI 1: var information: String;

 var script: $$anonymous$$ouseLookPlus;
 
 var script2: $$anonymous$$ouseLookPlus;
 
 private var guiOn = false;
 
 private var rect: Rect;
 
 function $$anonymous$$ouse1 () {
     
     script = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLookPlus);
     script.enabled = false;
     script2 = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLookPlus);
     script2.enabled = false;
 
 }
 
 function $$anonymous$$ouse11 () {
     
     script = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLookPlus);
     script.enabled = true;
     script2 = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLookPlus);
     script2.enabled = true;
     
 }
 
 function On$$anonymous$$ouseDown () {
     guiOn = true;
     rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
 }
 
 
 function OnGUI () {
 
     if (guiOn){
     
         $$anonymous$$ouse1();
         
         GUI.Label(rect, information);
         
         if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
                 guiOn = false;
         }
         if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
             animation.Play ("dooropen1");
             guiOn = false;
         }
         
     }
     
     else {
         $$anonymous$$ouse11 ();
     
     }
     
 }

GUI 2: var information: String;

 var script: $$anonymous$$ouseLookPlus;
 
 var script2: $$anonymous$$ouseLookPlus;
 
 private var guiOn = false;
 
 private var rect: Rect;
 
 function $$anonymous$$ouse2 () {
     
     script = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLookPlus);
     script.enabled = false;
     script2 = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLookPlus);
     script2.enabled = false;
 
 }
 
 function $$anonymous$$ouse22 () {
     
     script = GameObject.Find("$$anonymous$$ain Camera").GetComponent($$anonymous$$ouseLookPlus);
     script.enabled = true;
     script2 = GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLookPlus);
     script2.enabled = true;
     
 }
 
 function On$$anonymous$$ouseDown () {
     guiOn = true;
     rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
 }
 
 
 function OnGUI () {
 
     if (guiOn){
     
         $$anonymous$$ouse2();
         
         GUI.Label(rect, information);
         
         if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
                 guiOn = false;
         }
         if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
             animation.Play ("dooropen2");
             guiOn = false;
         }
         
     }
     
     else {
         $$anonymous$$ouse22 ();
     
     }
     
 }
avatar image gribbly · Mar 25, 2013 at 03:01 PM 0
Share

Please accept the answer that worked for you, and post a new question for this entirely separate issue!

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

11 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

Related Questions

Why this Error when trying to add a score using the new GUI ? 0 Answers

Gui.DrawTexture Working in Scene Mode, but not build or full game scene 2 Answers

Trying to get an RPC to display the health of a player over their head 0 Answers

NullReferenceException script problem? 0 Answers

NullReferenceException: Object reference not set to an instance of an object PlayerController.Update () (at Assets/Scripts/PlayerController.cs:19) Please Help 2 Answers


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