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
0
Question by Notae · May 04, 2012 at 02:23 AM · c#arrayclassinheritance

C# Array of Child Objects

In making an options menu for a game of mine, I've declared a variety of classes to hold each option. There are boolean-, numerical-, text-, colour-slider-, radio-button-, and list-type options. As such, I have classes declared more or less like so (with an enumeration 'opt' to contain the different option types):

 public class Option
 {
     public string title; // The label that appears in a list
     public opt type; // The type of option this is
 }

 public class OptionV : Option // This is for numerical value entry
 {
     public int digits;
     public bool negatable; // Information specific to each type
 }

 public class OptionL : Option // This type is to contain a list of other options
 {
     public Option[] list;

     public void Push (Option o)
     {
         // o is added to the list here
     }
 }

The problem I'm encountering is that I can't for the life of me figure out how to pass these different child types into the OptionL.Push method, or store the different types on OptionL.list. In C++ I know it was as simple as passing around Option pointers, but that doesn't appear to be an option so much anymore.

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
Best Answer

Answer by Bunny83 · May 04, 2012 at 02:31 AM

Native arrays can't be resized. They need to be recreated with the desired size. However It's better to use a container class like you would do in C++ since you can't resize arrays in c++ either. just import the System.Collection.Generic namespace and use a List:

 public List<Option> list = new List<Option>();

Just to prevent you from working into the wrong direction: A lot people recently tried to use custom classes (usual c# classes derived from System.Object) and want them to be serialized in an array or List of the base type. If you just use this at runtime there's no problem, but Unity can't serialize derived classes.

See those questions about that issue: http://answers.unity3d.com/questions/14877/can-a-custom-inspector-serialize-a-list-of-derived.html
http://answers.unity3d.com/questions/245604/does-unity-serialization-support-inheritence.html

Comment
Add comment · Show 2 · 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 Notae · May 04, 2012 at 03:46 PM 0
Share

Well my push function actually did generate a new array of incremental size, then copied all the elements over, but you're saying the generic list can be resized as simply as calling a built-in push function?

Also, you're saying the Generic List will store the child classes no problem, so long as I do it through code, and not through the inspector, correct?

avatar image Notae · May 09, 2012 at 09:11 PM 0
Share

Alright, it seems the composition approach described in one of those links has made the problem solvable (albeit with a sledgehammer approach). Thanks for the link.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Inheritance best solution to handle *many* instances of a class with unique values? 1 Answer

How to set different classes at runtime 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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