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 FarKing · Apr 15, 2013 at 12:35 AM · c#inheritancereturn

Returning variables from an inherited class

So i thought i had figured out a basic understanding of class inheritance. any method i have in the base class is useable in any class that inherits from it (i know that's the absolute simplest way of putting it)

so i have my Stars class that contains a hand full of variables and some functions to return these variables. then i have my different StarType class's (StarTypeA, StarTypeB)that inherit from the main Stars class, each with there own set of these variables( randomized depending on star type) but the StarType classes dont have the return functions in them (whats the point if they get that functionality from there base class right?)

now i cast a Physics.Raycast and get the

 StarTypeA sta = hit.transform.gameObject.GetComponent<StarTypeA>();

then call

 if (sta != null){
     type = sta.GetStarType();

i should be getting (or so i thought) a simple string from that game objects StarTypeA script via the base class

instead im getting nothing or to be more precise im getting the nothing that's stored in the base class and im not sure why.

any help, pointers or advice would be much very appreciated.

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

Answer by Chronos-L · Apr 15, 2013 at 01:21 AM

I am taking this as a opportunity to revise the concept in inheritance. So I have write a short set of codes to test it out. Hope that this will be able to help you in clearing some of your confusions over the matter.


BaseStar.cs

 using UnityEngine;
 
 public abstract class BaseStar : MonoBehaviour {
 
     public string _name;
     public int _temperature;
     protected int _estimatedCoreTemperature;
     
     public string GetName() {
         return _name;    
     }
     
     public int GetTemperature() {
         return _temperature;
     }
     
     public abstract int EstimateCoreTemperature();
 }
 



StarA.cs

 using UnityEngine;
 
 public class StarA : BaseStar {
 
     public override int EstimateCoreTemperature() {
         _estimatedCoreTemperature = _temperature * 2;
         return _estimatedCoreTemperature;
     }
 }
 



StarB.cs

 using UnityEngine;
 
 public class StarB : BaseStar {
 
     public override int EstimateCoreTemperature() {
         _estimatedCoreTemperature = _temperature * 5;
         return _estimatedCoreTemperature;
     }
 }



StarReader.cs

 using UnityEngine;
 
 public class StarReader : MonoBehaviour {
     
     //Plain Empty object with no component at all
     public GameObject emptyX, emptyY;
     
     // Use this for initialization
     void Start () {
         emptyX.AddComponent<StarA>();
         emptyY.AddComponent<StarB>();
         
         //Setting name from BaseStar
         emptyX.GetComponent<BaseStar>()._name = "RandomStarThatIsTypeA";
         emptyY.GetComponent<BaseStar>()._name = "RandomStarThatIsTypeB";
         
         //Setting temperature from Inherited star
         emptyX.GetComponent<StarA>()._temperature = 1000;
         emptyY.GetComponent<StarB>()._temperature = 1200;
         
         Debug.Log( 
             //GetName from base
             emptyX.GetComponent<BaseStar>().GetName() +
             "  " +
             //GetTemperature from sA
             emptyX.GetComponent<StarA>().GetTemperature() + 
             "  " +
             //CoreTemperature from sA
             emptyX.GetComponent<StarA>().EstimateCoreTemperature() 
         );
         
         Debug.Log( 
             //GetName from sB
             emptyY.GetComponent<StarB>().GetName() +
             "  " +
             //GetTemperature from sB
             emptyY.GetComponent<StarB>().GetTemperature() + 
             "  " +
             //CoreTemperature from Base, the matching implementation
             //in StarB is called because emptyY has starB
             emptyY.GetComponent<BaseStar>().EstimateCoreTemperature() 
         );
     }
 }
 
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 FarKing · Apr 15, 2013 at 02:11 PM 0
Share

As i was writing out a big post explaining why i didn't understand it suddenly clicked. Once again Chronos-L i am in your debt. (i think the L should stand for LEGEND)

also being able to use .GetComponent<>() on the base class is going to save a lot of stress later on

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Update base class variable value to pass it between subclasses already attached to game objects 2 Answers

type function not returning. c# 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