Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Stardog · May 27, 2011 at 01:08 AM · arrayinventorycheckindex

Check if an array index is empty

I'm trying to find the first empty spot in my array, then add something there. The problem is this code:

 if (inventory[i] == null)
 {
     inventory[i] = item;
     print("Added item");
     return;
 }

Without the check, it will add an item to the array, but only in the 1st spot of 6, no matter what's there. Removing the return makes it use all 6 slots of course.

With the null check, it doesn't meet the condition. I think it's because of the class I'm using?

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour {
     
     public InventoryItem[] inventory;
     
     [System.Serializable]
     public class InventoryItem
     {
         public GameObject worldObject;
         public Texture2D texRepresentation;
     }
     
     void Awake()
     {
         inventory = new InventoryItem[6];
     }
     
     public void AddItem(InventoryItem item)
     {
         for(int i = 0; i < inventory.Length; i++) 
         {
             if (inventory[i] == null)
             {
                 inventory[i] = item;
                 print("Added item");
                 return;
             }
         }    
         print("Inventory full");
     }
     
     public void NewItem(GameObject worldObject, Texture2D texRep)
     {
         var newItem = new InventoryItem();
         
         newItem.worldObject = worldObject;
         newItem.texRepresentation = texRep;
             
         AddItem(newItem);    
     }

 }
Comment
Add comment · Show 3
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 Jesse Anders · May 27, 2011 at 04:47 AM 1
Share

What do you mean by doesn't meet the condition? Do you mean that the '== null' test never evaluates to true, even when there are empty slots in the inventory?

avatar image FriskyDingo · May 27, 2011 at 11:08 AM 0
Share

I don't see any reason why AddItem wouldn't work as-is. The problem is likely where you're removing items from the list in some other code.

avatar image Stardog · May 27, 2011 at 12:02 PM 0
Share

Yes, I mean that it never evaluates to true even when the entire array is empty. I think it's because each Element of the array holds a GameObject/Texture2D, so I need to check if both those slots are null, but I'm not an advanced programmer.

1 Reply

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

Answer by Stardog · May 27, 2011 at 12:40 PM

Solved it. It was much simpler than I thought!

 if (inventory[i].worldObject == null && inventory[i].texRepresentation == null)
 {
     inventory[i] = item;
     print("Added item");
     return;
 }

Full script:

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour {
     
     public InventoryItem[] inventory;
     
     public bool displayInv = false;
     
     [System.Serializable]
     public class InventoryItem
     {
         public GameObject worldObject;
         public Texture2D texRepresentation;
     }
     
     void Awake()
     {
         inventory = new InventoryItem[6];
     }
     
     void Update()
     {
         if (Input.GetButtonDown("Inventory Key"))
         {
             if (!displayInv)
             {
                 print("Toggle Inv ON");
                 displayInv = true;
             }
             else
             {
                 print("Toggle Inv OFF");
                 displayInv = false;
             }
         }
     }
     
     public void AddItem(InventoryItem item)
     {
         for(int i = 0; i < inventory.Length; i++) 
         {
             if (inventory[i].worldObject == null || inventory[i].texRepresentation == null)
             {
                 inventory[i] = item;
                 print("Added item");
                 return;
             }
         }    
         print("Inventory full");
     }
     
     public void NewItem(GameObject worldObject, Texture2D texRep)
     {
         var newItem = new InventoryItem();
         
         newItem.worldObject = worldObject;
         newItem.texRepresentation = texRep;
             
         AddItem(newItem);    
     }
     
     void OnGUI()
     {
         if (displayInv)
         {
             GUI.Box(new Rect(Screen.width / 2, Screen.height / 2,250,250), "test");
         }
     }
     
 }
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

1 Person is following this question.

avatar image

Related Questions

Array items null but appear normal in inspector 2 Answers

IndexOutOfRangeExeption - Array index is out of range 2 Answers

Find index number in an Array of a transform by it's name 1 Answer

String - Replace Letters With Numbers 1 Answer

Checking Material and Destroying Another Object C# 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