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 /
avatar image
0
Question by d.b · Feb 13, 2013 at 06:21 AM · c#classinheritance

Inherited member variables not set to base value c#

Hi

I have a base class named Base that has a variable set in it to a value A second class inherits from this Base and im logging the variables value to the console in it but its not set to the value from Base, why? It recognises the member name, autocomplete will show me the member name before i have completed writing it, so it has been inherited, just not the value, why?

Any help would be appreciated, Thanks

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

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

Answer by iwaldrop · Feb 13, 2013 at 06:29 AM

Just because your inheriting class has the same member variables does not mean that it uses the value from a different instance of the base class. Something still has to set the instance of the extended class that you're using.

Comment
Add comment · Show 8 · 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 Bunny83 · Feb 13, 2013 at 07:19 AM 0
Share

The real conclusion is: We can't say anything without seeing your two classes. Your description is way too general / unclear.

avatar image d.b · Feb 13, 2013 at 10:56 PM 0
Share

fair enough and sorry for not being clear, i wrote the question at about 1.30am :s

 public class Base : $$anonymous$$onoBehaviour 
 {
     public float lifespan;

     // Use this for initialization
     void Start () 
     {
         lifespan = 3.0f;
     }
 }
 
 
 public class newClass : Base 
 {
     
     // Use this for initialization
     void Start () 
     {
         Debug.Log (lifespan); //this prints 0 not 3 like i       thought it should
     }
 }

thanks for your time

avatar image iwaldrop · Feb 13, 2013 at 11:30 PM 0
Share

That is to be expected. If you initialized lifespan in your declaration it would print '3'. There is a difference between the following;

 public float lifespan = 3;
 public float lifespan;
avatar image d.b · Feb 14, 2013 at 12:47 AM 0
Share

thanks that defiantly works (just tried it) but I read somewhere that variables should only be declared and not initialized out of the start function, is this true?

and one other question, why does it work when initialised at deceleration and not in start?

avatar image iwaldrop · Feb 14, 2013 at 01:36 AM 0
Share

You should not limit yourself to declaring variables at runtime; the popular reason for doing so is either the value is only known at runtime or because the developer wants to $$anonymous$$imize their memory footprint and not allocate memory for variables they're not using.

The reason is works is because the value is initialized! The start method is only called once a class has been instantiated as an object, but a value assigned to as its declared will always instantiate with its value. You could change it to anything you want at runtime, of course.

Show more comments

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

12 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

Related Questions

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

How to make a custom class inherited from Unity's GUI? 0 Answers

C# Conception - Hide inherited members and functions 1 Answer

Can I/Should I call Awake() in parent class manually? 1 Answer

Parent Class variables in Child Class's Inspector (C#) 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