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
1
Question by Life Alchemist · Sep 01, 2013 at 04:48 AM · inspectordatabaseinventorylistsitem

How do I retrieve an item from a list?

Alright, this is a little bit more complicated than a standard list call. I have already read through this link.

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

I have created a gameobject that has this ItemManager script attached to it in the scene.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ItemManager : MonoBehaviour {
     
     public List<Item> itemList = new List<Item>();
 
 }

And I also have this ItemManagerInspector script not attached to anything.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 
 [CustomEditor(typeof(ItemManager))]
 internal class ItemManagerInspector : Editor {
     
     bool showingWeapons = false;
     bool showingArmor = false;
     bool showingConsumables = false;
     
     
     public override void OnInspectorGUI()
     {
         ItemManager im = target as ItemManager;
         
         List<Weapon> weapons = new List<Weapon>();
         List<Armor> armors = new List<Armor>();
         List<Consumable> consumables = new List<Consumable>();
         
         for(int i = 0; i < im.itemList.Count; i++)
         {
             if (im.itemList[i].GetType() == typeof(Weapon))
             {
                 weapons.Add((Weapon)im.itemList[i]);
             }
             
             if (im.itemList[i].GetType() == typeof(Armor))
             {
                 armors.Add((Armor)im.itemList[i]);
             }
             
             if (im.itemList[i].GetType() == typeof(Consumable))
             {
                 consumables.Add((Consumable)im.itemList[i]);
             }
         }
         
         
         showingWeapons = EditorGUILayout.Foldout(showingWeapons, "Weapons"); //showingWeapons = changes bool by dropdown arrow
         
         if (showingWeapons == true) //while showingWeapons is true, make the list visible
         {
             EditorGUI.indentLevel = 2; //number of times to tab the following guitext
             //display all of the weapons in our database
             for (int i = 0; i < weapons.Count; i++)
             {
                 EditorGUILayout.BeginHorizontal();
                 EditorGUILayout.LabelField(weapons[i].name);
                 
                 if(GUILayout.Button("-"))
                 {
                     im.itemList.Remove(weapons[i]);
                 }
                 
                 EditorGUILayout.EndHorizontal();
                 
                 EditorGUI.indentLevel += 1;
                 weapons[i].name = EditorGUILayout.TextField("Name: ", weapons[i].name); //weapons[i].name changes variable when you type inside
                 weapons[i].description = EditorGUILayout.TextField("Description: ", weapons[i].description);
                 weapons[i].cost = int.Parse(EditorGUILayout.TextField("Cost: ", weapons[i].cost.ToString()));
                 weapons[i].damage = int.Parse(EditorGUILayout.TextField("Damage: ", weapons[i].damage.ToString()));
                 EditorGUI.indentLevel -= 1;
                 EditorGUILayout.Space(); //adds space between objects
             }
             
             if (GUILayout.Button("Add New Weapon"))
             {
                 Weapon newWeapon = (Weapon)ScriptableObject.CreateInstance<Weapon>();
                 newWeapon.name = "NEW WEAPON";
                 newWeapon.description = "";
                 newWeapon.cost = 0;
                 newWeapon.damage = 0;
                 //don't forget to add the other variable numbers
                 im.itemList.Add (newWeapon);
             }
             
             EditorGUI.indentLevel = 0; //makes sure only this dropdown is indented
             
         }
     }
 }
 

Please ignore the armor and consumable based stuff for now. I would like to know how to pull retrieve an item in the list from a completely different script such as an inventory script. For example in the inventory script if I wanted to use:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
     
     public List<Item> playerInventory = new List<Item>();
     public ItemManager im;
 
     playerInventory[5].name = im.itemList[13].name;
 }

What would I need to change? I also noticed the internal access for the ItemManagerInspector. This item database came from this youtube tutorial here

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by cdrandin · Sep 01, 2013 at 05:22 AM

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
  
 public class Inventory : MonoBehaviour 
 { 
     public ItemManager im = GameObject.Find(...).GetComponent<ItemManager>(); // In Find you will need to fill that in for which ever object the ItemManager is "attached" to.
     private List<ItemManager> = im.itemList;
 }

That will get you the inventory of the items you put in from your ItemManager class.

If there are any problems with it let me know, I have not tested it. If you want clarification just ask.

Comment
Add comment · Show 1 · 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 Life Alchemist · Sep 02, 2013 at 01:56 AM 1
Share

Ah, that's similar to what I worked out.

     public GameObject item$$anonymous$$anager;
     public Item$$anonymous$$anager im;
     
     void Start () {    
         im = item$$anonymous$$anager.GetComponent<Item$$anonymous$$anager>();
         Debug.Log (im.itemList[4].name);
         Debug.Log (im.itemList[4].description);
         Debug.Log (im.itemList[4].cost);
     }

I also found out that to access the armor list, it is consecutively after the weapon list so if weapon is filled up to 4 items (ie. up to array/list slot 3), and I created the armor list, I would simply call the slots afterwards (ie. 4,5,6,etc.)

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

16 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

Related Questions

Good way to store Items in a mySQL database? 2 Answers

Inventory Management Issue 2 Answers

*What are some ways of implementing a Mid Mission Weapons/Upgrade/Equipment store? 0 Answers

Custom Editor Window to Create and Store Item Data 1 Answer

Item database with custom editor 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