• 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 ClanDestiny · Feb 06, 2015 at 06:04 PM · parent

Variables Across Derivates

Hello,

I just need some clarification on something before I continue with this project. I have some public float variables set in a parent class. The child class is attached to an object.

In the parent class I set those variables in the declaration, i.e. public float headHealth = 80.0f; and public float accSwing = 0.70f;. The headHealth variable is then used and the value changed in a function contained in the parent. The value of accSwing does not get changed.

I have done some testing in various setups and here is what I noticed:

  • If the parent class is on the Game Object: Debugging the above variables returns the values I set them to in declaration.

  • If the derivate class is on the Game Object, the yet-untouched accSwing returns what I set it to, but headHealth returns 0. Note: Debugged from void Start() so no manipulations occurred yet.

  • In the same case as the previous point, but additionally the headHealth variable is later assigned a value in a called method contained in the parent class, it returns the value I set it to.

So my question is: Before I go re-arranging things in possibly convoluted and unneccessary ways, why is the declaration/initialization public float headHealth = 80.0f; in the parent class not carrying over to the derivate class' values of that variable, unless I set it in a method within the parent class? Conversely, why is public float accSwing = 0.70f; carrying over?

This is in part for my education but also to prevent future headaches. Thank you for your time!

Comment
Add comment · Show 6
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 JustAnotherDude · Feb 06, 2015 at 06:12 PM 1
Share

Can you post the code ?

avatar image steakpinball · Feb 06, 2015 at 10:19 PM 0
Share

What is the code of both classes?

avatar image ClanDestiny · Feb 07, 2015 at 08:08 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class Actor : $$anonymous$$onoBehaviour 
 {
     public string newTag;
     public float STR;
     public float AGI;
     public float END;
     public float INT;
 
     public float accSwing = 0.70f;
     public float headHealth = 80.0f;
     public float chestHealth;
 
     
     void Start() 
     {
         Debug.Log ("First off:" + headHealth);
         Debug.Log (accSwing);
         newTag = GetAllTags (transform);
         //Debug.Log (newTag);
         
         Debug.Log("Pre Set:" + headHealth);
         SetStats(newTag);
         Debug.Log("Post Set:" + headHealth);
     }
     
     // INITIALIZATION FUNCTIONS ==========
     
     public void SetStats(string tag)
     {
         SetAttributes(newTag);
         SetSkills(newTag);
     }
     
     private void SetAttributes(string tag)
     {
 
         if(tag.Contains("Human"))
         {
             STR = 1.0f;
             AGI = 1.0f;
             END = 1.0f;
             INT = 1.0f;
         }
 
         
         headHealth = headHealth * END;
         chestHealth = 100.0f * END;
                 
     }
     
     private void SetSkills(string tag)
     {
         
     }
     
     public string GetAllTags(Transform parent)
     {
         string newTag = "";
         
         foreach(Transform child in parent)
         {
             newTag = newTag + child.tag;
             Destroy(child.transform.gameObject);
         }
         //Debug.Log (newTag);
         return newTag;
     }
 }
 

Trimmed some of the unrelated stuff off, but those are the relevant parts. The object in question does not have this attached to it however, but rather the Player class as below:

 using UnityEngine;
 using System.Collections;
 
 public class Player : Actor 
 {
     /*void Start () 
     {
         SetStats(newTag);
         newTag = GetAllTags (transform);
         Debug.Log (newTag);
         Debug.Log(headHealth);
     }
     */
     void Update () 
     {
 
 
         if(Input.Get$$anonymous$$eyDown ("space"))
         {
             Debug.Log(headHealth);
         }
     }
 }


avatar image ClanDestiny · Feb 07, 2015 at 08:12 PM 0
Share

Note from the code in the last comment: Within SetAttributes on lines 49-50, I have one variable assigned and the other not: this is due to the process of assigning it at different stages to find a configuration that worked.

avatar image ClanDestiny · Feb 09, 2015 at 07:52 AM 0
Share

I'm going to close this since the proposed rule of thumb seems sufficiently effective for the encountered problem, even if I still don't understand what was going on there.

Thanks!

Show more comments

1 Reply

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

Answer by hav_ngs_ru · Feb 06, 2015 at 07:34 PM

as for me, I decided to follow simple rule: if I have a public variable - i dont ever trying to initialize it at declaration time. Either initilalize it in Awake/Start, or use serialized values (set in inspector). Set value at declaration works for non-serializable fields good, but not for public (serializable).

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 ClanDestiny · Feb 07, 2015 at 08:10 PM 0
Share

That's something to keep in $$anonymous$$d, though having looked up "serialization" a bit it's beyond my current understanding how it affects variables and access levels. It's odd though that at void Start() neither variable gets modified, but one works and the other doesn't.

avatar image JustAnotherDude · Feb 09, 2015 at 01:41 PM 0
Share

You do modify headHealth on start when you do :

headHealth = headHealth * END;

on SetAttributes < SetStats < Start

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Raycast always affects the same object 1 Answer

access grandfather of an object 3 Answers

[C#] How do I instantiate multiple objects at the same time? 1 Answer

Objects falls of the Moving Platform 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