• 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
Question by Omti1990 · Oct 03, 2018 at 09:34 PM · arraysinheritanceclassespolymorphism

Can I preserve the values in a parent class when I overwrite it with a child class?

Hello, my current problem is that I've got a class FieldInfo which has a child class called Building.

Now I generate FieldInfos for every field of my map during map generation. I store those FieldInfos in a 3d array FieldInfos[,,]. When I want to build a building I can stick the child class into the parent array. fieldInfos[x,y,z] = new Building(Construction.House);My problem here is that this would overwrite the values in fieldInfos (which are kind of important since they contain gameplay relevant map data) with the standard constructor of FieldInfos if I understand it correctly.

Is there some way to maintain the properties of the old FieldInfo object when I overwrite it with a Building object? Or would it make more sense to just make another array for the Building class and don't have it inherit from the FieldInfo class?

Comment

People who like this

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

Answer by JVene · Oct 04, 2018 at 05:21 AM

All you really need to do is pass the values to be preserved in the original FieldInfos[] to the constructor of the Building so it copies those values into its base. There's no way to 'preserve' the original FieldInfos, the Building is a separate instantiation, it can't slice the derived part onto an existing instantiation of the base.


That said, the question that comes to my mind is a mystery from my viewpoint, since I don't get any information about your design - but is a Building a FieldInfo? More to the point, not just from the standpoint of data, but from the standpoint of member functions, does a Building need any of the member functions of the FieldInfo base in order to function correctly as a Building?


If the answer reveals that a Building is otherwise so separate from the methods and data of FieldInfo that you're merely tacking the Building onto a FieldInfo because that base has pertinent bits of data but is otherwise not a functional base component, then perhaps the relationship isn't inheritance, but one of ownership. Perhaps a Building should be something that attaches to an existing FieldInfo, and can use the FieldInfo through an interface that communicates between them.


From this distance I have no information that informs me which is the truth in your case, but such is the nature of object design where the basic question is to differentiate between things that "are a" (or "is a") and those things that "have a" class. If shape is a base, then obviously circles, squares and triangles ARE shapes. That satisfies the "is-a" relationship, and indicates the circle, square and triangle may well derive from the shape base. However, consider the relationships established in Unity itself (they are well chosen) where components are added to a GameObject because they are "owned" not "comprised of" the attached classes. Derivation is an intimate relationship, whereas ownership is causal and requires a protocol for interaction (and that limitation is functional, lending a design a more solid foundation).

Comment
Omti1990
Bunny83

People who like this

2 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 Omti1990 · Oct 04, 2018 at 08:12 AM 0
Share

Thank you very much for the explanation. You're right, a Building doesn't require all the Information of a FieldInfo only a bunch of them. (And sometimes most of them).

Maybe just attaching the building class to the FieldInfo would be more efficient. Might be sensible to just copy the bits of data that are required for my Buildings into the classes before I attach them. Would it make sense to use Interfaces here? (Never used those before)

avatar image JVene Omti1990 · Oct 04, 2018 at 11:05 AM 1
Share

I don't think interfaces are indicted here.


When I used the word 'interfaces', I was speaking more generically about the way classes interact with each other. I'm primarily a C++ programmer, though like many in the field I know and use several languages, and the word 'interface' is uniquely a keyword in C# in this case, it is more a generic term for a C++ programmer (and others), and to a gray beard like me, C# is a new language ;)


Attaching the relevant data into Building may result in faster operation when using that data in Building as opposed to giving Building reference to the 'owner' FieldInfo, but either will do and is a matter of tuning you'll be in a better position to recognize (with the code in front of you).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

95 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 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 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 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 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 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

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

Polymorphism question for my custom class 1 Answer

How to make an array inherit from a class? 2 Answers

Abstract class question 1 Answer

Using AddComponent to add a Sub Class using a String 4 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