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 fernandovt · Oct 25, 2013 at 06:48 PM · guiobjectnullreferenceexception

Problem with Null Reference Exeption??:/

Hello:), so this script is meant to deactivate a SafeLock Script at first, and then when the player hits "E", the SafeLock Script gets activated (Shows a keyboard(GUI) where you write the right combination to unlock a safe). The proble is that the activation and deactivation of the SafeLock Script is working (when I press "E" near the safe it shows the GUI). But all the other variables that i wanted affected(walking, moving the camera, run, turn my flashlight on/off), are not being affected.... Unity displays an error that says:

"NullReferenceException: Object reference not set to an instance of an object SafeLockOnE.Update () (at Assets/SafeLockOnE.js:37)"

So when the player aproaches the safe and press "E", the Gui gets displayed, but i can still walk, move the camera, run, turn my flashlight on/off, etc...What should I do?? Please Help

This is the Scipt:

 var range: float = 5; 
 private var hit: RaycastHit;
 var SafeLockMeca : SafeLock;
 
 var lookAround01 : MouseLook;
 var lookAround02 : MouseLook;
 var charMotor : CharacterMotor;
 var charMotor2 : FPSWalkerEnhaced;
 var FlashlightHide : FlashLight;
 var HideCursor : HideMouseCursor;
 
 function Start ()   
 {
     SafeLockMeca = gameObject.GetComponent(SafeLock);
     
     lookAround01 = gameObject.GetComponent(MouseLook);
     lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
     charMotor = gameObject.GetComponent(CharacterMotor);
     charMotor2 = gameObject.GetComponent(FPSWalkerEnhaced);
     FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight);
     HideCursor = GameObject.Find("Player").GetComponent(HideMouseCursor);
 
         SafeLockMeca.enabled = false;
 
 }
 
 
 
 function Update(){
 
   if (Input.GetKeyDown("e")){
     var ray = Camera.main.ViewportPointToRay(Vector3(0.5,0.5,0));
     if (Physics.Raycast(ray, hit, range)){
 
         SafeLockMeca.enabled = true;
         
         lookAround01.enabled = false;
         lookAround02.enabled = false;
         charMotor.enabled = false;
         charMotor2.enabled = false;
         FlashlightHide.enabled = false;
         HideCursor.enabled = false;
 
         
 
 
 }
 }
 }
 
Comment
Add comment · Show 2
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 fafase · Oct 25, 2013 at 07:05 PM 0
Share

Concerning you Null reference exception, where is it happening? Is it on FlashLightHide?

avatar image fernandovt · Oct 25, 2013 at 07:18 PM 0
Share

From line 37 to line 42 are not working, i can tell because line 35 works and displays the GUI of the Safe(keyboard), but the other things (lookaround, char motor, etc) are not working (because I can still move the camera, walk, etc). I tested deleting for example line 37, 16 and 5(everything related to lookAround01), and the same error appears, only that this time reffering to the next thing, and the next thing, etc... The only thing that works is the "SafeLock$$anonymous$$eca" thing...Any Ideas??

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jacksmash2012 · Oct 25, 2013 at 07:26 PM

This line here in your Start function:

FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight);

Make sure you throw in a debug statement right after the fact that ensure you grabbed the reference and that FlashlightHide is not null at this point.

Comment
Add comment · Show 1 · 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 fernandovt · Oct 25, 2013 at 07:39 PM 0
Share

Actually from line 37 to 42 doesn't work, and that's really weird because in an almost identical script, everything works for me, but in this one, it doesn't. The similar, but working script:

 var lookAround01 : $$anonymous$$ouseLook;
 var lookAround02 : $$anonymous$$ouseLook;
 var char$$anonymous$$otor : Character$$anonymous$$otor;
 var char$$anonymous$$otor2 : FPSWalkerEnhaced;
 var FlashlightHide : FlashLight;
 var HideCursor : Hide$$anonymous$$ouseCursor;
 
 var respawnTransform : Transform;
 
 static var playerIsDead = false;
 function Start ()   

 {
     lookAround01 = gameObject.GetComponent($$anonymous$$ouseLook);
     lookAround02 = GameObject.Find("$$anonymous$$ainCamera").GetComponent($$anonymous$$ouseLook);
     char$$anonymous$$otor = gameObject.GetComponent(Character$$anonymous$$otor);
     char$$anonymous$$otor2 = gameObject.GetComponent(FPSWalkerEnhaced);
     FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight);
     HideCursor = GameObject.Find("Player").GetComponent(Hide$$anonymous$$ouseCursor);
 
 
 }
 
 function Update ()  

 {
     if (playerIsDead == true)
     {
         lookAround01.enabled = false;
         lookAround02.enabled = false;
         char$$anonymous$$otor.enabled = false;
         char$$anonymous$$otor2.enabled = false;
         FlashlightHide.enabled = false;
         HideCursor.enabled = false;
 
 
        
     }
 }
 
 function OnGUI ()
 {
 if (playerIsDead == true)
 {
     if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40), "Respawn"))
     {
         RespawnPlayer();
     }
     
     if (GUI.Button(Rect(Screen.width*0.5-50, 240, 100, 40), "$$anonymous$$enu"))
     {
         Debug.Log("Return to $$anonymous$$enu");
     }
   }
 }
 function RespawnPlayer ()
 {
      transform.position = respawnTransform.position;
      transform.rotation = respawnTransform.rotation;
      gameObject.Send$$anonymous$$essage("RespawnStats");
      lookAround01.enabled = true;
      lookAround02.enabled = true;
      char$$anonymous$$otor.enabled = true;
      char$$anonymous$$otor2.enabled = true;
      FlashlightHide.enabled = true;
      HideCursor.enabled = true;
      playerIsDead = false;
      Debug.Log("Player has respawned");
 }
 @script RequireComponent(CharacterController)

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

16 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 avatar image avatar image avatar image

Related Questions

How to draw GUI Text from code 1 Answer

Object reference exists but also doesn't? 1 Answer

NullReferenceException in for-loop 0 Answers

How to disable my Script and Gui 1 Answer

Trying to create a new script in javascript error 0 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