0
votes

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:

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, ScreenGrab prefabStar 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)

1
What line of code does the error message indicates? Seems like your prefabStar isn't set properly and points to null/nothing. Can you Debug.Log it before instantiation to see if it points to anything meaningful? - Eric
@Rixment, the line which says "var newStar = Instantiate(...)" in the Star class - SidS
What does the prefabStar points when you Debug.Log it before Instantiation? - Eric
Debug Logging prefabStar returns a null, this is odd because I've attached the prefab to this variable through the editor - SidS
I find it a little bit difficult to debug your case without a little more info. Leave the field where you assign the prefabStar on the star class as a public one, make the component attached to prefabStar serializable and see if it is null upon starting the game. Be aware that AddComponent will create any references to other Components as null. By the way, In your scenario, I would probably create an object pool using something like Zenject so I could create all objects I will need at startup. - cesartalves

1 Answers

0
votes

Check the editor again and try to look if the prefab you've attached is still visible in the game editor upon starting the game. Maybe mistakenly you've attach it in the editor while being in game-mode.

Also if you've made your change in the edit mode to your object that is also a prefab you need to apply the changes to your prefab by clicking on Apply button, that will be located most probably in the upper right corner of your editor.

From the screenshot that you've posted I would choose the prefab that is in question from the bottom part of your screen. Upon selection the right side panel should change to show your prefab data. Then I would lock the right side panel by clicking on little lock icon that is visible at the top. While locked just press and drag your prefabs from the bottom panel to appropriate places in your right side panel.