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 thousandcranes · Aug 27, 2016 at 05:12 PM · inheritanceclasses

Set value of a variable of a parent class - c#

I have a base abstract class Hero with a protected int health in it. I have a second class Mage which inherits Hero. I want to set a health value for the mage. health = 5; -> not working, it says its a field but is used as a type What do i do? How do i set values for all the types of heroes?

Comment
Add comment · Show 3
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 DiegoSLTS · Aug 27, 2016 at 05:16 PM 0
Share

What you said you're doing is O$$anonymous$$. A protected field is visible for derived classes, and assignment works like always, so you're probably doing something more, share the exact code you're using and the exact error message.

avatar image thousandcranes · Aug 28, 2016 at 05:39 PM 0
Share

@DiegoSLTS public abstract class Hero : $$anonymous$$onoBehaviour { protected int health; }

public class $$anonymous$$age: Hero { health = 10; }

Thats what im doing. :( Help!

avatar image thousandcranes thousandcranes · Aug 28, 2016 at 05:39 PM 0
Share

What you said you're doing is O$$anonymous$$. A protected field is visible for derived classes, and assignment works like always, so you're probably doing something more, share the exact code you're using and the exact error message.

2 Replies

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

Answer by thousandcranes · Aug 29, 2016 at 04:54 AM

holy smokes! you should set value in the constructors! like this: Child class:

public Mage () { health = 10; }

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
2

Answer by DiegoSLTS · Aug 28, 2016 at 06:26 PM

You can't override the health value like that. You have to set a different value inside some function, like Awake for example:

 public abstract class Hero : MonoBehaviour {
     protected int health;
 }

 public class Mage: Hero {
     void Awake() {
         health = 10;
     }
 }

 public class Archer: Hero {
     void Awake() {
         health = 15;
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Inheriting from classes from other files (Javascript) 1 Answer

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

Defining and inheriting from a Javascript Class 0 Answers

Class extends Object but does not inherit variables 1 Answer

Can't inherit from namespace 0 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