- Home /
Read different game objects into array at same time?
How do I read different GameObjects into a GameObject array all at once in C#? I tried the syntax "GameObject[] EnemyWaveW1 = new int[] { 1, 3, 5, 7, 9 };" but it is throwing me an error. Unexpected Symbol '{'
public class EnemySpawnerScript : MonoBehaviour {
//These are assigned to prefabs through the inspector
public GameObject ENM_1;
public GameObject ENM_2;
public GameObject ENM_3;
GameObject[] EnemyArrayW1 = new GameObject[3];
// Use this for initialization
void Start () {
//I COULD read in each GameObject separately, but how do I do it all at once?
EnemyArrayW1[0] = ENM_1;
EnemyArrayW1[1] = ENM_2;
EnemyArrayW1[2] = ENM_3;
}
I'm not sure what you mean, here. Logically, there isn't any way of doing this, unless you know some pattern of how the objects are going to appear, or are using some function like GetComponentsInChildren! As, @gruhm says, for your specific example, you should just make the array public and do it in the inspector, but in a more general case what you are trying to do here is really not straightforward.
Answer by gruhm · Oct 21, 2011 at 11:54 PM
You could simply make the array of gameobjects public and drop them in via the inspector:
public GameObject[] EnemyArrayW1;
exactly, that's what arrays are made for :D. If you have seperate variables they can't be treated as one thing (well with reflection it would be possible but it wouldn't be simpler then using just an array).
Answer by TukangSendal · Nov 05, 2019 at 07:13 AM
just add simply array, and go drop your game object via inspector
public GameObject[] EnemyArray;