- Home /
Problem with inheritance and access between scripts.
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.
Answer by salex100m · Nov 17, 2013 at 07:22 PM
I'm not 100% sure but i think you might not be using the solider as intended.
base.unit = x;
That just creates a reference in your file pointing to another reference. But neither reference is actually pointing to an "actual" object. You might need to instantiate a solider, THEN set it to base.unit = x.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
A node in a childnode? 1 Answer
Disable Mac OSX 'Active Screen Corners' 2 Answers
Multiple Cars not working 1 Answer