- Home /
Question by
theUndeadEmo · Jan 25, 2012 at 10:30 PM ·
arraytagchange
changing the tags in an array
This is my current script, would like to get the gameobjects into an array then, change the tags of each object in the array, based on the index .
private GameObject[] enemies;
//private GameObject enemy;
void Start ()
{
enemies = GameObject.FindGameObjectsWithTag ("Enemy");
//enemy = GameObject.FindWithTag ("Enemy");
foreach (GameObject enemy in enemies) {
switch (enemies) {
case 0:
enemy.tag="enemy1";
break;
case 1:
enemy.tag="enemy2";
break;
case 2:
enemy.tag="enemy3";
break;
}
}
}
Comment
Best Answer
Answer by theUndeadEmo · Jan 25, 2012 at 10:53 PM
problem solved, here's the answer any future trouble, its not very good coding as i have to hardcode each index but it works for now:)
private GameObject[] enemies;
//private GameObject enemy;
void Start ()
{
enemies = GameObject.FindGameObjectsWithTag ("Enemy");
//enemy = GameObject.FindWithTag ("Enemy");
for ( int i=0; i<enemies.Length; i++ )
{ enemies[0].tag= "enemy1"; enemies[1].tag= "enemy2"; enemies[2].tag= "enemy3";
}
}
for(int i = 0 ; i < enemies.Length ; i++){
enemies[i].tag = "enemy" + (i + 1).ToString() ;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613063410im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array without taged objects but without couting itself 1 Answer
"For"-Loop only works for final element in array 1 Answer
why does my code tag objects it's not supposed to? 0 Answers
How do I change a tag? 1 Answer
Array loads in wrong order 3 Answers