- Home /
Instantiating a gameObject in an array is not working for me
I'm making a game which lets you spawn units in a game, and to start spawning a unit I need to instantiate the unit prefab first to then get the necessary components from their individual variables. However I can not instantiate the gameObject into the array.
public GameObject[] instantiatedUnits;
float[] unitLastSummonTime = new float[5];
float[] unitCooldowns = new float[5];
float[] unitCosts = new float[5];
public void InstantiateAllUnits()
{
for (int i = 0; i < 5; i++)
{
instantiatedUnits[i] = (GameObject)Instantiate(Resources.Load("Units/" + UnitManagement.unitsName[i].ToString()), new Vector3(0, 3, 0), new Quaternion());
unitCooldowns[i] = instantiatedUnits[i].GetComponent<UnitScript>().cooldown;
unitCosts[i] = instantiatedUnits[i].GetComponent<UnitScript>().cost;
Destroy(instantiatedUnits[i]);
}
}
When I try to instantiate outside of the array, it works. But I need it in the array so I can call select the unit from it. Any help would be great.
from what you've shown it's not apparent where the problem might lie, your code seems to be ok, apart from the fact that I can't see where you initialize an array. I'm guessing it's a serialized field on a $$anonymous$$onoBehavior? I also don't understand what do you mean by "it works". how do you call this method? more info is needed.
Answer by Arshww · Feb 11, 2020 at 04:03 AM
Well, i'd do this using List<> instead of array[], here's how i'd do it:
//First, using List is way better for adding the units instantiated:
public List<GameObject> instantiatedUnits = new List<GameObject>(); // Changed from array[] to List<>. !!REMEMBER TO ADD THE NAMESPACE: using System.Collections.Generic!!
float[] unitLastSummonTime = new float[5];
float[] unitCooldowns = new float[5];
float[] unitCosts = new float[5];
public void InstantiateAllUnits()
{
for (int i = 0; i < 5; i++)
{
//now here we create a GameObject named "UNIT" and we instantiate the unit you'd like:
GameObject UNIT = Instantiate(Resources.Load("Units/" + UnitManagement.unitsName[i].ToString()), new Vector3(0, 3, 0), new Quaternion());
//then we add the UNIT GameObject to the instantiatedUnits List:
instantiatedUnits.Add(UNIT);
//DONE! Now you can access your instantiated Unit and its properties like in arrays: instantiatedUnits[Index]
unitCooldowns[i] = instantiatedUnits[i].GetComponent<UnitScript>().cooldown;
unitCosts[i] = instantiatedUnits[i].GetComponent<UnitScript>().cost;
Destroy(instantiatedUnits[i]);
}
}
I have changed it to a List but it did not fix the problem. However I did realize that my Destroy line might have been deleting the objects from the array ins$$anonymous$$d of just killing them off and leaving instances for me to clone from and by removing that it seems to have fixed it.
Your answer
Follow this Question
Related Questions
2D Array of GameObjects... 1 Answer
2D Array's 1 Answer
Instantiating from an object that's in an array 1 Answer
A null value was found where an object instance was required. 1 Answer