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 Celtican · Jun 03, 2018 at 04:55 PM · inheritancecastingoop

Upgrading a Custom Class into an Inheriting Class

Let's say my game has a party system, and perhaps a storage for characters which are not in the player's party (such as a town). When a character is in storage, there are only a few properties it needs, perhaps "Name," "Strength," and a list of abilities would do.

 public class Character
 {
      public string Name;
      public int Strength;
      public Ability[] Abilities;
 }

This is fine and all, and won't take up too much space when saving the game. But now our poor, poor character joins our party and needs new properties. I've created a larger class which inherits the Character class and adds some neat stuff to work with.

 public class PartyCharacter : Character
 {
      public int CurHealth;
      public Enemy Target;
 }

Here's where my problem rises; true to the title, I'm uncertain how to transform a Character class into a PartyCharacter class. This is what I'd like to do:

 Character JohnDoe = new Character();
 PartyCharacter Leader = (PartyCharacter)JohnDoe;

Needless to say that doesn't work. Possibly noteworthy: I'd like to create a constructor for both classes, both of which would calculate/randomize the character's stats. Also, how would transforming a PartyCharacter to a Character work?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Horschty · Jun 04, 2018 at 07:55 PM

How about this:

 public class Character {
   public string Name;
   // Copy constructor which allows you to make a copy of itself
   public Character(Character other) {
     Name = other.Name;
   }
 }

 public class PartyCharacter : Character
 {
    public int CurHealth;
    public Enemy Target;

   // Here you call the base copy constructor,
   // which then copies the passed in characters fields into THIS instance
   public PartyCharacter(Character character) : base(character) {
   }
 }

 ...
 Character character = new Character();
 character.Name = "Peter";
 PartyCharacter partyCharacter = new PartyCharacter(character);
 // partyCharacter.Name should now also be Peter
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
avatar image
0

Answer by ShadyProductions · Jun 04, 2018 at 08:32 PM

Horschty's answer is the most accurate to get what you want to achieve.

It's impossible to cast Character to it's derived class.

For example, I can't cast a car into a ferrari, it might be a honda.

But you can however cast from your derived class to your base class.

For example, I can cast this honda into a car, because it's a car.

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

86 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

Related Questions

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

Attach a non Monobehavior class to a GameObject 1 Answer

Does Unity support multiple Inheritance? 3 Answers

Prefab inheritance without scripts. Is it possible? 2 Answers

OO Design | Specific example 2 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