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.

1 Answer

1

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() 
		);
	}
}

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