- Home /
Find GameObjects With different Tag
Hi! I need find GameObjects With different Tag and put it in the one array, i'll try do it this way: (but it write me, that "Array index is out of range")
var waypoints: GameObject[] ;
var player2: GameObject[] ;
var player3: GameObject[] ;
function Update () {
player2 = GameObject.FindGameObjectsWithTag("Player2");
player3 = GameObject.FindGameObjectsWithTag("Player3");
waypoints[0] = player2[0] ; //Error here: Array index is out of range
waypoints[1] = player3[0] ;
}
Maybe anyone know other way how can i do this?
Have you initialized the waypoints array to be the correct size?
Initialized? $$anonymous$$mm, just this way "var waypoints: GameObject[]", it's not enough? How can i set correct size?
Answer by FL · Jan 10, 2013 at 01:09 PM
Like whydoidoit said, the array needs to be initialized. Change
var waypoints: GameObject[] ;
to
var waypoints: GameObject[] = new GameObject[2];
For a 2 position builtin arrays. You can also initialize by the editor (put a size number like 2) or use non-fixed size arrays too.
Answer by Lovrenc · Jan 10, 2013 at 01:12 PM
var waypoints: GameObject[] ;
var player2: GameObject[] ;
var player3: GameObject[] ;
function Update () {
player2 = GameObject.FindGameObjectsWithTag("Player2");
player3 = GameObject.FindGameObjectsWithTag("Player3");
waypoints = new GameObject[player2.length + player3.length];
waypoints[0] = player2[0] ; //Error here: Array index is out of range
waypoints[1] = player3[0] ;
}