- Home /
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
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.
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.)