- Home /
Make an array containting multiple classes
I have 2 classes a weapon class and and Item class. The weapon class extends/inherits the Item class. I would like to make an inventory array or list to store all my items. Is there a way to make an array or list of 2 different things
EX
var inv = new ArrayList(weapon,Item)
inv.Add(a gun)
inv.Add(a non gun item)
Answer by Slobdell · Jul 17, 2013 at 11:01 PM
If you set the arraylist up as an item arraylist you can add weapons to it as well since they are items. However you will either have to know if it is supposed to be a weapon when you take it out and cast it as such, or check which class type it is first and then and cast it.
Works but the weapon in the array only has the property's defined in the Item class, not the weapon class
Woops. never $$anonymous$$d, it only displays the Item property's but if I ask it to do something with the Weapon propertys, such as debug the weapons type it works Thanks, Also I change it form Array list to generic list
Or do you mean when you take it out it only has the item properties and not the weapon properties? You have to cast it back.
Weapon w = (Weapon)items[itemIndex]
When I look in the inspector for the List it only displays the variables I declared in the Item Class, but that dose not matter. Whenever I ask the array to say call a function/method located in the weapon class it works fine. Thanks
Your answer
Follow this Question
Related Questions
How to use Javascript Array with classes? 1 Answer
How to find the opposite variables list? 1 Answer
Inspector variable MonoScript, using for creating a List? 2 Answers
Shotgun using array/list in Javascript 1 Answer
A node in a childnode? 1 Answer