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 /
This question was closed Jul 06, 2014 at 10:40 PM by QuestionAsker for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by QuestionAsker · Jul 01, 2014 at 01:45 AM · prefabclassinheritancevalueprivate

How to save prefabs with private data through inheritance

I have a 2 class inheritance chain, Builing - House. I want to create a house with values set from the classes but they can't be public, which means I can not edit them in the inspector. So my questions is, how do I set private values so I can use my inheritance tree properly?

Extra info: If I make another class, say SmallHouse, as a child of House then I can not add the SmallHouse script to a list of Houses which is important. It is actually why I need so hard code this information somewhere.

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

  • Sort: 
avatar image
1
Best Answer

Answer by Maerig · Jul 01, 2014 at 02:28 AM

The protected modifier makes a field visible for a class and its derived classes only, is it what want ?

Comment
Add comment · Show 4 · 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 QuestionAsker · Jul 01, 2014 at 02:30 AM 0
Share

Well I am using protected but the issue is this class seems redundant.

  public class SmallHouse : House {
         //All of these propeties are set through the inherited protected members.
         public SmallHouse ()
         {
             this.buildingName = "Small House";
             this.costWood = 750;
 
 /*properties omitted due to repitivity.*/
         }
     }
 

I made this class to actually give the properties some value but there has to be a better way to do it.

avatar image Maerig · Jul 01, 2014 at 02:45 AM 2
Share

I think there is nothing wrong with your implementation, but in C# you could also do this using abstract or virtual properties, although I'm not sure which solution is better. Anyway, if this is more to your liking, here is how you would do it :

 // Father class
 protected abstract string BuildingName {
     get;
 }
 
 protected abstract int CostWood {
     get;
 }
 
 
 // Child class
 protected override string BuildingName {
     get {
         return "Small House";
     }
 }
 
 protected override int CostWood {
     get {
         return 750;
     }
 }
avatar image QuestionAsker · Jul 01, 2014 at 03:16 AM 0
Share

Thats works great!! Thank you!!

avatar image Kiwasi · Jul 01, 2014 at 04:05 AM 0
Share

You can also do this without using abstract members and simply assign them values in the child class in a constructor (or Awake if using $$anonymous$$onoBehaviour).

It really depends if you are going to have Houses running around as well as SmallHouses.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Private variable is not saved 3 Answers

Can I save class instances in a prefab-like way? 1 Answer

Trying to pass a float value from a prefab to another script that compares values from multiple prefabs 1 Answer

How should one give a gameobject to a prefab? 1 Answer

C# Question - Do I Have to inherit from MonoBehaviour? What happens if I don't? 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