Create a copy of a non MonoBehaviour class and set variables from other class
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;
     }
 }
 
              When you write planet = new Planet$$anonymous$$anager[2];, all C# knows is that you are going to use that array to store references to two Planet$$anonymous$$anager objects. Those objects do not yet exist. 
Your answer
 
             Follow this Question
Related Questions
"The associated script cannot be loaded" 0 Answers
Script errors 1 Answer
"The associated script cannot be loaded." 0 Answers
Сonstructor return null 0 Answers