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 chrismcrae5712 · Feb 25, 2014 at 06:31 PM · javascriptnullreferenceexception

Null Reference Exception - Object Not Set

I have followed JesseEtzler's Advanced Inventory Tutorial on YouTube. However I added my own touch to the script (which works fine) Just the way he referenced his objects is differen than how I need to do mine. My question is how can I achieve this? Thanks In advance.

Inventory Script Also is the EquipMenu script.

 import System.Collections.Generic;
 
 var items : Item[];
 
 var MainInventory : List.<Item> = new List.<Item>();
 
 var EquipMenu : Item[];
 
 
 
 
 function Start () {
     MainInventory.Add(items[0]);
     MainInventory.Add(items[1]);
     MainInventory.Add(items[2]);
     MainInventory.Add(items[3]);
     MainInventory.Add(items[4]);
 
 }
 
 function Update() 
 {
     
 }
 
 
 
 function OnGUI ()
  {
  
 
 
     for(var x = 0; x < MainInventory.Count; x++) {
             if(GUI.Button(Rect(Screen.width/2 + 300, Screen.height/2 + 25 + (20 * x), 100, 20), "" + MainInventory[x].Name)) {
                 if(MainInventory[x].itemType == MainInventory[x].itemType.Cuirass ) {
                     if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
                         if(MainInventory[x].itemID == MainInventory[x].itemID.IronCuirass ){
                             EquipMenu[0] = MainInventory[x];
                             MainInventory.RemoveAt(x);
                             EquipMenu[0].IsEquipped = true;
                         }
                     }
                 }    
                 else if(MainInventory[x].itemType == MainInventory[x].itemType.Helm ) {
                     if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
                         if(MainInventory[x].itemID == MainInventory[x].itemID.IronHelm ){
                             EquipMenu[1] = MainInventory[x];
                             MainInventory.RemoveAt(x);
                             EquipMenu[1].IsEquipped = true;
                         }
                     }
                 }
                 else if(MainInventory[x].itemType == MainInventory[x].itemType.Greaves ) {
                     if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
                         if(MainInventory[x].itemID == MainInventory[x].itemID.IronGreaves ){
                             EquipMenu[2] = MainInventory[x];
                             MainInventory.RemoveAt(x);
                             EquipMenu[2].IsEquipped = true;
                         }    
                     }    
                 }
                 else if(MainInventory[x].itemType == MainInventory[x].itemType.Boots ) {
                     if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
                         if(MainInventory[x].itemID == MainInventory[x].itemID.IronBoots ){
                             EquipMenu[3] = MainInventory[x];
                             MainInventory.RemoveAt(x);
                             EquipMenu[3].IsEquipped = true;
                         }
                     }
                 }
                 else if(MainInventory[x].itemType == MainInventory[x].itemType.Gauntlets ) {
                     if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
                         if(MainInventory[x].itemID == MainInventory[x].itemID.IronGauntlets ){
                             EquipMenu[4] = MainInventory[x];
                             MainInventory.RemoveAt(x);
                             EquipMenu[4].IsEquipped = true;
                         }    
                     }
                 }                
             }
         }
     
     for(var y = 0; y < EquipMenu.length; y++) {
         if(EquipMenu[y] != null) {
         
             if(GUI.Button(Rect(Screen.width/2 + 410, Screen.height/2 + 25 + (20 * y), 100, 20), "" + EquipMenu[y].Name)) {
             
                 
                     MainInventory.Add(EquipMenu[y]);
                     EquipMenu[y].IsEquipped = false;
                     EquipMenu[y] = null;
                     
                 
             }
         }
     }
 }

Here is my Renderer Script that tells the Skinned Mesh Renderer when to be true or false This calls for a "true" or "false" statement from the Inventory and equipmenu

 public var enabled : boolean;
 
 renderer.enabled = false;
 
 function Update() {
             
             if(GameObject.Find("Human_M_Full1").GetComponent("Inventory").EquipMenu[0].IsEquipped) {
                 renderer.enabled = true;
             }
             else if(!GameObject.Find("Human_M_Full1").GetComponent("Inventory").EquipMenu.IsEquipped) {
                 renderer.enabled = false;
             }    
         }


  • Here's the Exception Error I get* This happens for each piece of armor (mesh) I have *

    NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)

By the way this only happens when I Unequip the Item and try to reset my EquipMenu[y] back to null.

EquipMenu[y] = null;) that's when the error occurs.

Any help is greatly appreciated! Thanks guys!

Comment
Add comment · Show 6
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 perchik · Feb 25, 2014 at 06:33 PM 0
Share

What line does the exception point to?

avatar image chrismcrae5712 · Feb 27, 2014 at 01:00 AM 0
Share

It actually points to

  if(GameObject.Find("Human_$$anonymous$$_Full1").GetComponent("Inventory").Equip$$anonymous$$enu[0].IsEquipped) {
           renderer.enabled = true;
avatar image Aridez · Feb 27, 2014 at 02:03 AM 1
Share

I didn't go through all the code but maybe if its not equiped you have to check first if: GameObject.Find("Human_$$anonymous$$Full1").GetComponent("Inventory").Equip$$anonymous$$enu[0] == null or GameObject.Find("Human$$anonymous$$_Full1").GetComponent("Inventory") == null and dont do anything in case it is.

avatar image chrismcrae5712 · Feb 27, 2014 at 05:48 AM 0
Share

Thanks for the the Reply Aridez, this is actually what I was wondering. Was how to implement a way to check to see if I could check the null status. I'm gonna try to add that I didn't know it had to be format "==" lol I'll feel so stupid if that's all I was doing wrong. :p

avatar image chrismcrae5712 · Feb 27, 2014 at 07:01 AM 1
Share

Oh man! Took a lot of toying around, but your idea worked! I got it all working properly man thank you so much!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

"NullReferenceException" Error when trying to count objects with a particular tag using GameObject.FindGameObjectsWithTag function 1 Answer

Another Null Reference Exception 1 Answer

Help with eval() 0 Answers

NullReferenceException 1 Answer

NullReferenceException: Object reference not set to an instance of an object 3 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