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
0
Question by 4illeen · Aug 10, 2011 at 07:20 PM · listinventoryinheritanceitems

List doesn't hold children's variables

I've got an Item class which holds the following variables: string Name, float Weight, int Value.

I've also got few item classes which inherit from Item such as Weapon, Armor, Food, Ingredients. They all store some variables suitable for those items (such as attack power, armor rating). I wanted to create an inventory using List<Item> but I found out it doesn't store Weapon, Armor, etc variables, nor can I read them via their getters.

Is there a way I can store all my items on one list so that I don't have to create a list for each item type?

I'm a C# guy by the way.

Comment
Add comment · Show 1
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 4illeen · Aug 10, 2011 at 08:36 PM 0
Share

I need to know if it's even possible, or should I just make many lists

1 Reply

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

Answer by Bunny83 · Aug 10, 2011 at 10:11 PM

Of course you can store all those items in the same list, but you need to cast it into the correct type before you can access specific members of the real type. That's how it works in most OOP languages.

 List<Item> myList = new List<Item>();
 
 myList.Add(new Weapon());
 myList.Add(new Armor());
 myList.Add(new Weapon());
 
 foreach(Item I in myList)
 {
     if (I is Weapon)
     {
         Weapon W = (Weapon)I;
         W.RateOfFire = 5;
     }
     // or 
     if (I is Armor)
     {
         ((Armor)I).rating = 10;
     }
 }

That's all plain C# and nothing Unity special. The only thing you have to be careful is that Unity can't serialize a list or array of a base-class that contains derived classes. Unity serializes the classes by the variable type and not the actual type. As long as you use the List only at runtime to store your items there's no problem.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How i can make a script-made button interactable? 0 Answers

How to ignore base class? 1 Answer

A node in a childnode? 1 Answer

How to make an inventory for my text advenutre? 0 Answers

Lists, Is a type but is used like a variable Problem C# 1 Answer


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