Repopulating an Array after GameObject is destroyed in game
So far, I have not been able to re-spawn a GameObject to put back into the array it was destroyed from. I am using a DestroyByContact script to do the destroying.
I know I need to search the array for null values and then spawn one more and place it in the array, but I can't quite get my syntax right to make it happen.
Below is what I'm working with
public GameObject alienPrefab;
public GameObject[] alienArray;
void Start () {
for (int i=0; i < Random.Range(5,20); i++){
GameObject go = alienPrefab;
Vector3 v = new Vector3 ();
v.x = Random.Range (-50, 50);
v.z = Random.Range (-50, 50);
Instantiate (go, v, Quaternion.identity);
go.transform.position = v;
go.AddComponent (typeof(Mutation));
}
alienArray = GameObject.FindGameObjectsWithTag ("Alien");
}
void Update () {
foreach (GameObject go in alienArray) { //this cycles through the array
Mutation mut = (Mutation)go.GetComponent (typeof(Mutation));
if (mut == null) {
//this is where I think I'm suppose to put my syntax
break;
}
Debug.Log (go.name);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612141045im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object reference not set to an instance of an object + array of positions 1 Answer
Need to randomly place Waypionts from an Array in an "closed" 2D space 0 Answers
Spawning limited GameObjects at a specific position not working 1 Answer
How to make an array that stores x, y and z co-ordinates? 1 Answer