Hi! I currently learn scripting basics. Can someone tell me, why I got this error message (NullReferenceException: Object reference not set to an instance of an object) to the following code?
public class StarSystemManager : MonoBehaviour {
public PlanetManager[] planet;
void Start()
{
planet = new PlanetManager[2];
planet[0].name = "planet0"; // At this point.
planet[0].Setup(10); // But at this point I also got the same message.
}
}
public class PlanetManager { // I don't want to set it to MonoBehaviour
private int orbit;
public string name;
public void Setup(int distance)
{
orbit = distance;
}
}
I don't understand, why can't I use a function or set a variable in a copy of a class, what I defined. I defined an array with copies, not? And I can't use it anyway. It works only, if I use parameters with constructor:
public class StarSystemManager : MonoBehaviour {
public PlanetManager[] planet;
public PlanetManager tempPlanet;
void Start()
{
planet = new PlanetManager[2];
tempPlanet = new PlanetManager(10, "planet0");
planet[0] = tempPlanet;
}
}
public class PlanetManager {
private int orbit;
private string name;
public PlanetManager(int distance, string planetName)
{
orbit = distance;
name = planetName;
}
}
↧