• 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
Question by fernandovt · Oct 26, 2013 at 03:22 AM · raycastnullreferenceexeption

Problem with Null Reference Exception??

So i have a script that activates a GUI when you press "e" on it. It's meant to pause the game when you are writing a "secret password" on the GUI. For this, i did the same that I have done with another script, I lock the camera, the movement of the character, the hability to turn the flash light on/off, etc. The problem only one thing is working, when i press "e" it gets displayed, but all the other variables that i want to transform into "false" are not responding... Here is the error:

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

Here is the Script:

 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;
        
                     
   if (Input.GetKeyDown("KeyCode.Escape")){
 
         SafeLockMeca.enabled = false;
 
 
 }
 }
 }
 }

Tips: If i delete line 38, and everything that it's related to it, the next line does'nt work either, so only "SafeLockMeca.enabled = true;" it's working, from lookAround01.enabled = false; to HideCursor.enabled = false; is not working...:(

PS:

1) Also i don't know why but i discovered that the GUI with the password gets displayed also if i press "e" near any object with a collider...

2) I would also like if someone can show me a way to close the GUI when i press "ESC"

Comment

People who like this

0 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 fernandovt · Oct 26, 2013 at 06:02 AM 0
Share

Yes that's already done, but it still show the same error...:/

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by aldonaletto · Oct 26, 2013 at 06:50 AM

Which object is this script attached to? This code could get the right script instances only if all of them (including this script) were attached to the same object - the player, in this case. If this script (and SafeLock) are attached to a different object than the player, Start should be like this:

 function Start ()  
 {
     SafeLockMeca = GetComponent(SafeLock); // get SafeLock from this object
     var player = GameObject.Find("Player"); // get other scripts from the player
     lookAround01 = player.GetComponent(MouseLook);
     lookAround02 = Camera.main.GetComponent(MouseLook);
     charMotor = player.GetComponent(CharacterMotor);
     charMotor2 = player.GetComponent(FPSWalkerEnhaced);
     HideCursor = player.GetComponent(HideMouseCursor);
     // be careful: if there's more than one flashlight object
     // in scene, this line may return the wrong instance:
     FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight);
     SafeLockMeca.enabled = false;
 }

And the Raycast won't do what you expect, whatever it is: it checks for key E whenever any collider is in the middle of the screen and closer enough to the camera. What do you wanna do? Sense E when the mouse is over an existing GUIText or GUITexture? Or when the mouse is over the area where the GUI password should appear?

EDITED: Ok, so you want to enable the GUI code when the player is inside the trigger and E is pressed. You could use OnTriggerStay, but it's more efficient to have a boolean that tells whether the player has entered/exited the trigger. You should also monitor the current GUI state, and enable/disable the other scripts only when the GUI gets disabled/enabled - like this:

 private var inTrigger = false; // is player inside trigger?
 
 function OnTriggerEnter(other: Collider){
   if (other.CompareTag("Player")) inTrigger = true;
 }
 
 function OnTriggerExit(other: Collider){
   if (other.CompareTag("Player")) inTrigger = false;
 }
 
 private var guiEnabled = false; // current gui state
 
 function Update(){
   // if inside trigger and E pressed...
   if (inTrigger && Input.GetKey("e")){
     SafeLockMeca.enabled = true; // enable gui
   }
   // ESC disables gui
   if (Input.GetKey("escape")){ 
     SafeLockMeca.enabled = false;
   }
   // if GUI changed its enabled state...
   if (SafeLockMeca.enabled != guiEnabled){
     guiEnabled = SafeLockMeca.enabled; // update guiEnabled
     // disable/enable other scripts accordingly:
     lookAround01.enabled = !guiEnabled;
     lookAround02.enabled = !guiEnabled;
     charMotor.enabled = !guiEnabled;
     charMotor2.enabled = !guiEnabled;
     FlashlightHide.enabled = !guiEnabled;
     HideCursor.enabled = !guiEnabled;
   }    
 }
 

NOTE: In order to close the GUI when the correct password has been entered, just make SafeLock disable itself - for instance (SafeLock script):

 function OnGUI(){
   password = GUI.TextField (Rect (10, 10, 200, 20), password, 25);
   if (password == "cheat123"){
     enabled = false; // disable itself
   }
 }

The script in the trigger detects any change in SafeLock.enabled property and disables/enables the other scripts as needed.

Comment
fernandovt

People who like this

1 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 fernandovt · Oct 26, 2013 at 07:05 AM 0
Share

The GUI pasword script is attached to a cube with a trigger, it will be ok if when the player is on the trigger and presses "e", just then the GUI gets displayed, but i have no clue how to do it, i have been experimenting with something like this, but i don't know if i'm doing it right, or how to implement it:

 function OnTriggerEnter (Col : Collider)
 {
 if(Col.tag == "Player")
 {

.....

avatar image fernandovt · Oct 26, 2013 at 07:13 AM 0
Share

And if it's possible, to close the GUI on "Esc" key or when you write the password correctly or incorrectly (close it in both cases)

avatar image fernandovt · Oct 26, 2013 at 05:31 PM 0
Share

Aldonaletto...,everything worked, you solved all my problems:):). I really apreciate your time and help. I learned a lot with you. Thanks for your patience and your goodwill:)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Gun locator script isnt working 1 Answer

RayCast Shooting not working 0 Answers

Yet another NullReferenceException question. 2 Answers

C# Parsing Error and Unexpected Symbol 1 Answer

Problem with raycast shooting 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