So I’m making an RTS game and I’m running into problems with inheritance. I’m going to strip to the basics to make the question more simple.
So I have four scripts,
Building_Main.cs
BuildingA.cs which inherits from building_Main
Base_Unit.cs
Soldier.cs - which inherits from Base_unit.
Building_Main has some methods that draw gui and builds the building queue which work perfectly.
So Building A’s start code looks like
public GameObject x;
void Start () {
base.unit = x;
}
so x is the unit building A will produce. x is a gameobject in my assets folder, with the soldier.cs script attached. I attached building_A to an actual gameobject in the world and dragged the prefab soldier from my asssets and put it in x.
This is the Base_Unit script
protected float health { get; set; }
protected float speed { get; set; }
protected int level { get; set; }
internal float buildTime { get; set; }
void Start () {
}
// Update is called once per frame
void Update () {
}
This is the Soldier.cs which inherits
void Start () {
base.health = 10;
base.speed = 10;
base.defense = 10;
base.buildTime = 10;
}
As you can see, soldier sets its building time to be 10.
The problem occurs in Building_main’s addToQueue function whose code is below:
public void addToQueue(GameObject bug)
{
buildQueue.Add(bug);
Debug.Log("Adding to the queue, a build time of " + unit.GetComponent<Base_Unit>().buildTime);
}
Remember unit is what building_A sets to soldier in its start function.
However buildtime comes to be 0 even thought it should be 10.
Am I approaching this wrong? I have no idea how to proceed further. Please let me know if the question isn’t clear enough.