I'm trying to instantiate multiple objects called Stars. In my Simulation class, I have a method where I am creating an object called newStar and attaching the class Star.
Here is my GameObject hierarchy:
The Simulation script, which has the Simulation class is attached to the Simulation gameobject. The Star script which has the Star class is attached to the StarObject gameobject. Within the Star script, I've attached both the prefab of the star to prefabStar and the StarObject gameobject has been attached to the StarObject variable through the Unity editor.
Here is my Simulation class: (ObjectStar is a GameObject which is the parent of the prefabStar.
void listStars()
{
int i = 0;
while (i < (StarDataBank.Instance.NumOfStars))
{
int primaryID = int.Parse(StarDataBank.Instance.StarIDID[i]);
string properName = StarDataBank.Instance.StarName[i];
string HIPID = StarDataBank.Instance.StarIDHIP[i];
string HDID = StarDataBank.Instance.StarIDHD[i];
string HRID = StarDataBank.Instance.StarIDHR[i];
string GLID = StarDataBank.Instance.StarIDGL[i];
string BFID = StarDataBank.Instance.StarIDBF[i];
decimal rightAscension = Convert.ToDecimal(StarDataBank.Instance.StarRA[i]);
decimal declination = Convert.ToDecimal(StarDataBank.Instance.StarDec[i]);
decimal Mag = Convert.ToDecimal(StarDataBank.Instance.StarMag[i]);
decimal CI = Convert.ToDecimal(StarDataBank.Instance.StarCI[i]);
int scale = 0;
int r = 0;
int g = 0;
int b = 0;
Star newStar = ObjectStar.AddComponent<Star>();
newStar.Instantiate(primaryID, properName, HIPID, HDID, HRID, GLID, BFID, rightAscension, declination, Mag, CI);
i++;
}
}
Here is the Star class:
public void Instantiate(int primaryID, string properName, string HIPID, string HDID, string HRID, string GLID, string BFID, decimal rightAscension, decimal declination, decimal magnitude, decimal colourIndex)
{
this.primaryID = primaryID;
this.properName = properName;
this.HIPID = HIPID;
this.HDID = HDID;
this.HRID = HRID;
this.GLID = GLID;
this.BFID = BFID;
this.rightAscension = rightAscension;
this.declination = declination;
this.magnitude = magnitude;
this.colourIndex = colourIndex;
var newStar = Instantiate(prefabStar, transform.position + getVector(Convert.ToDecimal(rightAscension), Convert.ToDecimal(declination)), Quaternion.identity);
newStar.name = (primaryID).ToString();
newStar.transform.parent = StarObject.transform;
newStar.transform.localScale = new Vector3(20, 20, 20);
}
My desired intention is to create a clone of the prefabStar, with each clone attached to the Star class, so I can retrieve the variables of each clone of the prefabStar.
Instead, what's happening is I'm getting an error that reads "The Object you want to instantiate is null." and there are no new gameobjects (prefabStar isn't being cloned.)
EDIT: I have added a screengrab of the Star prefab in the editor,
Notice how the Star class is attached to this prefab. (Please ignore the name Star Manager, I was doing a few tests, the problem still persists with it called Star)
