- Home /
Set a Tag Multiple GameObject[]
How can i set a tag multiple GameObject[] i tried this but its not work.
public GameObject[] randomSpawnes;
void SetTag(string tag ){
GameObject[] randomSpawnes = GameObject.FindGameObjectsWithTag (tag);
foreach (GameObject target in randomSpawnes)
{
target.gameObject.tag ="BlaBla";
}
}
Tags should also be set up in the tag manager before use. Is "blabla" assigned in Tag manager?
What "does not work", are you sure BlaBla
is an existing tag in the editor? You can't create tags at runtime. The tag must exist in the list of tags of the Editor.
Did you try debugging? What is the length of randomSpawnes? Also you have declared randomSpawnes twice, once locally and other globally.
Answer by Koyemsi · Apr 07, 2018 at 04:22 PM
Hi. So you're trying to re-tag objects that were already tagged ? Or did I understand wrong ?
Anyway, I think your script can't work because :
you declared twice randomSpawnes as an array of GO
you never call the SetTag() method which does the tagging.
Your script should be something like that :
public class Retagging : MonoBehaviour {
public GameObject[] randomSpawnes;
void Start () {
SetTag ("blabla", "jobijoba");
}
void SetTag (string oldTag, string newTag) {
randomSpawnes = GameObject.FindGameObjectsWithTag (oldTag);
foreach (GameObject target in randomSpawnes) {
target.tag = newTag;
}
Debug.Log (randomSpawnes.Length + " objects found and re-tagged");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612160252im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Match 3 game getting the colour of the match 1 Answer
nullReferenceException when comparing tag in a search for a specific parent. help. 1 Answer
Making object switch between two tags every 3 seconds 3 Answers
Using FindGameObjectsWithTag to print text 1 Answer
Raycast isn't working when with tag 2 Answers