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 Ouija · Feb 23, 2014 at 03:01 AM · doorkeyopenactivate

Activate object with key

Hey guys, can anyone help me figure out what is happening here? I keep getting this error on the script. Driving me bananas. I apologize for the structure of the code, I'm trying to use notepad because mono will not open for some reason. Notepad is giving me a hard time, please help me figure out whats wrong. :)

NullReferenceException: Object reference not set to an instance of an object

devilFood.Awake () (at Assets/_myscripts/myScript.cs:29) public GameObject laser;
public bool requireKey;
public AudioClip doorSwishClip;
public AudioClip accessDeniedClip;
private DoneHashIDs hash;
private GameObject player; private DonePlayerInventory playerInventory;
private int count;

void Awake () {
hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent(); player = GameObject.FindGameObjectWithTag(DoneTags.player); playerInventory = player.GetComponent(); } void OnTriggerStay (Collider other) { if(other.gameObject == player) { if(requireKey) { if(Input.GetButtonDown("Interact")) if(playerInventory.hasKey) LaserDeactivation(); count++; else { audio.clip = accessDeniedClip; audio.Play(); } } else count++; } else if(other.gameObject.tag == DoneTags.enemy) { if(other is CapsuleCollider) count++; } } void OnTriggerExit (Collider other) { if(other.gameObject == player || (other.gameObject.tag == DoneTags.enemy && other is CapsuleCollider)) count = Mathf.Max(0, count-1); } void LaserDeactivation () { laser.SetActive(true); audio.Play(); } }
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 Truger · Feb 23, 2014 at 05:11 AM 0
Share

Around line 29 your missing a closing bracket i believe

avatar image mattyman174 · Feb 23, 2014 at 07:01 AM 0
Share

If your going to omit brackets when using single line after an IF statement, can you please properly format your code.

You are missing a bracket.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by joseques · Feb 23, 2014 at 07:12 AM

Check your brackets. Your missing open and closed brackets on line 28 and 35

Also I give you a link to format your C# code for better readability. http://www.manoli.net/csharpformat/

Here's your fixed and formated code

 public GameObject laser;       
     public bool requireKey;                  
     public AudioClip doorSwishClip;           
     public AudioClip accessDeniedClip;          
     private DoneHashIDs hash;                
     private GameObject player;
     private DonePlayerInventory playerInventory;    
     private int count;                    
     
     void Awake ()
     {
         
         hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneHashIDs>();
         
         player = GameObject.FindGameObjectWithTag(DoneTags.player);
         
         playerInventory = player.GetComponent<DonePlayerInventory>();
         
     }  
     void OnTriggerStay (Collider other)
     {   
         if(other.gameObject == player)
             
         {      
             if(requireKey)           
             {
                 
                 if(Input.GetButtonDown("Interact"))
                 {
                     if(playerInventory.hasKey)
                         LaserDeactivation();
                     count++;
                 }
                 else          
                 {          
                     audio.clip = accessDeniedClip;            
                     audio.Play();          
                 }
             }
             
             else       
                 count++;
         }   
         else if(other.gameObject.tag == DoneTags.enemy)     
         {     
             if(other is CapsuleCollider)                    
                 count++;
         } 
     }   
     void OnTriggerExit (Collider other) 
     {     
         
         if(other.gameObject == player || (other.gameObject.tag == DoneTags.enemy && other is CapsuleCollider))  
             count = Mathf.Max(0, count-1);
     }   
     
     void LaserDeactivation ()     
     {        
         laser.SetActive(true);   
         audio.Play();
     }
 }
Comment
Add comment · 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

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

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

Related Questions

Opening Door with Key 1 Answer

Door opening with a key, my scripts and problem 1 Answer

key to open gate 1 Answer

Press an in-game switch to open a door - help? 2 Answers

GUI text, door and object collider 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