Getting variables from subclasses

Hi everybody. I’m having problems working with subclasses, like the next example:

I have those scripts:

.-A controller script (_thugController.cs) that manages the movement of enemies and variables (speed, jump, etc.).

.-A subclass controller script (_flyController.cs) for aereal enemies.

.-An action script (_flyActions.cs).

My “flyController.cs” script has their own variables, like ex.:

using UnityEngine;
using System.Collections;

public class _flyController : _thugController {

	public Vector3 trajectory;
	public float accel;
	public float deccel;
	void Start () {


And I need “_thugActions.cs” get those variables:

public class _flyActions : _thugActions {

	public enum eneState{Patrol, Attack, Stop};
	public eneState state;

	public _thugController thugCon; //Used to access _flyController.cs

	// Use this for initialization
	void Start () {

		state = eneState.Patrol;
		thugCon.accel = 4f;
		thugCon.deccel = 15f;

But then, the script can’t get both “accel” and “deccel” because “thugCon” is being read like the Main Class, and not like the “fly” SubClass.

Is there any solution other than creating a “_flyController.cs” variable?

The answer is no, and this is by design, in the c# language.

Here’s the reason:

You’ve defined a class called “Thug”, and a subclass “Fly”. You’ve said that “Fly” has accel and decel, but not all types of Thug have those vars.

Therefore, if you have a reference type of “Thug”, the compiler doesn’t know which type of Thug it is, and so it cannot allow your code to assume it’s a “Fly” type with the accel & decel values - so you can’t refer to them.

One option would be to test if it’s a “Fly” type, and re-cast it if so.
Another option would be to use the specialised subclass as your variable type, as you suggested.

I’m going to show you an example of re-casting types. Your naming conventions are (to me) a bit messed up, so here’s an example using unity’s standard convention: Capitalised for classes, lowercase for variables. Here I define a class called “Food”. It’s edible by default, and has an “Eat” function.

using UnityEngine;
public class Food : MonoBehaviour {

    public bool edible { get; protected set; }
    protected virtual void Awake() { edible = true; }

    public void Eat() {
        if (edible) {
        } else {
            Debug.Log ("Yuck.");

Next we have a subclass called “Meat” which starts off inedible (overriding the base function), but has a cook function to make it edible.

using UnityEngine;
public class Meat : Food {
    // meat is inedible until cooked
    protected override void Awake () { edible = false; }

    public void Cook() {
        edible = true;
        Debug.Log ("Cooked meat.");

Finally the “Person” class has a food variable. It wants to eat the food, but has to test to see if it’s the subclass Meat (in which case it recasts the Food as Meat, and Cook()s it before eating!).

using UnityEngine;
public class Person : MonoBehaviour {
    public Food food;

    void Start() {

    public void Eat(Food food) {
        if (food is Meat) {
            Meat meat = (food as Meat);

Based on what you are doing, changing the variable to _flyController makes the most sense.

Parent classes can’t know anything about their subclasses, but sub-classes know everything the parent class knows. By declaring it as _flyController, you still have access to the functionality of _thugController. It doesn’t work the other way around.

You can cast _thugController reference to _flyController and access its members…

void Start () {

    state = eneState.Patrol;

    var flyController = (_flyController)thugCon;
    flyController.accel = 4f;
    flyController.deccel = 15f;