• 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 Razputin · Feb 10, 2017 at 06:47 AM · inheritanceextendgettersgetter

Using Getters Question

I'm trying to use extensions for the first time in unity and I'm a bit confused on the implementation.

I have

 public class Stats : MonoBehaviour{
 
     public int intelligence;
     public int agility;
     public int strength;
     public int health;
     public int damage;
     public int speed;
 
     public Stats()
     {
         intelligence = 0;
         agility = 0;
         strength = 0;
         health = 0;
         damage = 0;
         speed = 0;
     }
 
     public int getIntelligence()
     {
         return intelligence;
     }
     public int getAgility()
     {
         return agility;
     }
     public int getStrength()
     {
         return strength;
     }
     public int getHealth()
     {
         return health;
     }
     public int getDamage()
     {
         return damage;
     }
     public int getSpeed()
     {
         return speed;
     }

and in another script I have

 public class Creature : Stats {
 
     void Start () {
         intelligence = getIntelligence();
         strength = getStrength();
         agility = getAgility();
         health = getHealth();
         damage = getDamage();
         speed = getSpeed();
     }
 

I have set the "Stats" in the inspector to all be 1 so I can see them change from 0 to 1 however t$$anonymous$$s doesn't work. Is t$$anonymous$$s now how you do t$$anonymous$$s? I feel like I'm not understanding the proper way to implement t$$anonymous$$s. Could someone explain?

Thank you!

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

Answer by bburtson09 · Feb 10, 2017 at 07:38 AM

What you're looking for is somet$$anonymous$$ng called properties. Properties are similar to member variable fields. they use special get and set keywords. Properties are great and promote encapsulation.

For a $$anonymous$$gh level brief intro to properties see : https://unity3d.com/learn/tutorials/topics/scripting/properties?playlist=17117

Note-Properties don't by default show in the inspector.

Additionally, I see that your class creature inherits from stats. T$$anonymous$$s means that your member variables agility, health, speed. Live and are accessable in your creature class even though not visible.

Eg. If your creature class had no implementation or member variables you could still access its (Stats) member variables like t$$anonymous$$s in another script.

 Creature myCreature = gameObject.GetComponent<Creature>();
 myCreature.agility = 25;

Remember when using polymorp$$anonymous$$sm you are saying, "Creature, IS a Stat, but Stat IS NOT a creature.

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 Razputin · Feb 10, 2017 at 07:42 AM 0
Share
avatar image bburtson09 Razputin · Feb 10, 2017 at 07:54 AM 0
Share
avatar image
0

Answer by HarshadK · Feb 10, 2017 at 07:31 AM

You are using constructor in a Monobehaviour derivative script. You are not supposed to do it as that clashes with serializer and is invitation to trouble. You can use method like Awake() or Start() to initialize.

Comment
Add comment · Show 1 · 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 Razputin · Feb 10, 2017 at 07:42 AM 0
Share

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

64 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

Related Questions

C# Extending Script causes InvalidCastException on Instantiate 1 Answer

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

Does the Unity Inspector support Inheritance? 1 Answer

How to "include" an inherited C# Class into the file? 3 Answers

C# Inheritance Problems 1 Answer


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