- Home /
Accessing base class list in a derived class.
I need to get information on a list I made in a base class inside of a derived class. Is there anything special I need to do? Whenever I try to get the list.count the value just returns 0 every time. I've gone through the code letter by letter and nothing is wrong. list is public, semicolons, no misspellings. Help?
public class ShipBehavior : MonoBehaviour
{
public List<Vector3> position = new List<Vector3>();
protected void Start ()
{
SetPoints();
}
protected void SetPoints ()
{
position.Add(new Vector3(-2f, -0.9f, -4.5f));
position.Add(new Vector3(0f, -1.2f, -4.5f));
position.Add(new Vector3(2f, -0.9f, -4.5f));
}
}
public class BarrelParent : ShipBehavior
{
void Start ()
{
InvokeRepeating("Spawn", 2, 1.0f);
}
void Spawn ()
{
//outputs as 0
Debug.Log(position.Count.ToString());
}
}
Answer by zach-r-d · Jul 05, 2015 at 07:43 PM
This is happening because the parent class's Start(), and thus SetPoints() as well, is never being called.
Make Start() in the parent class protected (or public):
protected void Start ()
then call base.Start()
as the first line of the child class's Start() method.
I tried this and the debug.log still outputs as 0. I'll update the code.
The updated code does indeed have the parent class's Start method marked protected, but base.Start() must still be called as the first line of the child class's Start() method.
Your answer
![](https://koobas.hobune.stream/wayback/20220611225844im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Multiple Cars not working 1 Answer
Getting a list in a derived class 1 Answer
Distribute terrain in zones 3 Answers
How to ignore base class? 1 Answer