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 Voridian · Jul 20, 2014 at 05:08 AM · c#errorngui

Object Reference Not Set As Instance error from ngui code

hey all, not sure if many of you use ngui but il ask anyway. im using unity with the free version of ngui and im still quite new to it. my code gives this console error:

"NullReferenceException: Object reference not set to an instance of an object NGUITools.SetActive (UnityEngine.GameObject go, Boolean state) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:613) CharacterControler.UseInventory () (at Assets/Script/CharacterControler.cs:288) CharacterControler.Update () (at Assets/Script/CharacterControler.cs:51)"

is this a ngui issue or has my code gone wrong? my code is here if you need it:

 void Update () {
 if(Input.GetButtonDown("Inventory") && canInventory){
             canInventory = false;
             StartCoroutine(InvTimer(1));
             UseInventory();
 }
 }
     void UseInventory(){
         GameObject HudPanel = GameObject.Find("HudPanel");
         GameObject InventoryPanel = GameObject.Find("InventoryPanel");
         if(!inInventory){
             Debug.Log("inInventory");
             Screen.lockCursor = false;
             NGUITools.SetActive(InventoryPanel,true);
             //NGUITools.SetActive(HudPanel,false);
             inInventory = true;
         }
         else if (inInventory){
             Debug.Log("not inInventory");
             Screen.lockCursor = true;
             NGUITools.SetActive(InventoryPanel,false);
             //NGUITools.SetActive(HudPanel,true);
             inInventory = false;
         }
     }
     public IEnumerator InvTimer(float delay){
         yield return new WaitForSeconds(delay);
         canInventory = true;
     }
         }
 
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 alwayscodeangry · Jul 20, 2014 at 06:22 AM 0
Share

Best guess is that InventoryPanel is not found, so you may be passing a null reference into NGUITools.SetActive(). That being said, I had a quick look at the function source in NGUI, and it does appear to do a null check itself, so I'm a little confused :( Are you using the most up to date version of NGUI?

avatar image Voridian · Jul 20, 2014 at 06:27 AM 0
Share

nah i dont have $60 spare so im just using the free version

avatar image alwayscodeangry · Jul 20, 2014 at 06:36 AM 0
Share

All I can really suggest is that you do a null check on InventoryPanel, and make sure it's named correctly in the scene.

avatar image Voridian · Jul 20, 2014 at 06:58 AM 0
Share

it is, otherwise i wouldnt be able to do a NGUITools.SetActive(InventoryPanel, false); the problem (i think) is that if its disabled then i cant access it and enable it because it the properties cease to exist when its disabled if that makes any sense at all

1 Reply

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

Answer by alwayscodeangry · Jul 20, 2014 at 07:02 AM

Yes, you're actually pretty close. I should have spotted this first time round, but GameObject.Find() won't work on an inactive GameObject.

Try finding the object on Awake and holding a reference, or set a public reference via the inspector. It'll be more efficient than calling GameObject.Find() at game time as well :)

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 Voridian · Jul 20, 2014 at 07:09 AM 0
Share

Ah i got it working now. Thanks so much

avatar image alwayscodeangry · Jul 20, 2014 at 10:04 AM 0
Share

Not a problem :)

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

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

Related Questions

Distribute terrain in zones 3 Answers

C# script error. 0 Answers

Show Strings With Last Characters of the first Word 1 Answer

error CS8025: Parsing error in C# code 3 Answers

Null in GetValidMethodInfo 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