- Home /
How do you change the tag of an Object in runtime?
Please I am desperate, how do I change a tag during runtime: I tried what I knew from Unity 4- I have a tagged Object "Tag1" and have a second tag "Tag2"; Then I use this: void Update() { gameObject.tag = "Tag2"; } and during runtime it switches between "Tag1" and "Tag2"- but its not possible now:( , help me,please.
What exactly is going wrong?
What you're doing is perfectly correct. I just tested it in Unity 5.3.0f4 and it works just fine. Just make sure that you're defined "Tag2" in the Tag $$anonymous$$anager.
Answer by mightybob · Dec 25, 2015 at 01:07 AM
If you declare the tag in the tag manager, then
gameObject.tag = "DeclaredTag";
should work fine.
I did find the problem- after a lot of time I realized the problem wasn't in the "gameObject.tag = "Declared Tag";". It was in my system- this is working with if() but not with void On$$anonymous$$ouseDown()- but I really need to do it with if clicked on my object system, any help on that?
Answer by tanoshimi · Dec 24, 2015 at 04:58 PM
You have a typo on line 8. You're assigning the tag "UnitSelected", but testing the tag "UnitSected".
That's not the problem I typed it just now- the system isn't working for my script:/...
When you're asking for help with faulty code, please don't introduce further errors by then mistyping the code here. Post your actual code, together with any actual error messages.
What does "it will change the tag, but however it is not working it stays tag1" mean? And if it's "not working now", when did it last work?
Please post a screenshot of the tags defined in your project.
Answer by OgiJr · Dec 24, 2015 at 04:37 PM
The script- using UnityEngine; using System.Collections;
public class Test : MonoBehaviour {
void OnMouseClick()
{
gameObject.tag = "UnitSelected";
}
void Update()
{
if (gameObject.tag == "UnitSelected")
{
transform.position = new Vector3 (20,5,0);
}
}
} I want whenever I click on the object - it will change the tag, but however it is not working it stays tag1 and I am not sure what does defined a tag mean- I mean is it to create a tag?
Answer by Deadcow_ · Dec 24, 2015 at 11:54 AM
private void Start()
{
tag = "Tag2";
if (CompareTag("Tag2")) Debug.Log(name + " tag is now Tag2");
}
Cartainly works, if you declared "Tag2" in the TagManager.
What specifically do you mean by "not possible now"? What error do you have?
Your answer

Follow this Question
Related Questions
Controlling visibility of several objects with GUI 2 Answers
Changing the direction of an object using another object. 0 Answers
Is it possible to capture each rendered frame without missing frames? 2 Answers
Best Motion Capture Gloves for Realtime use in Unity 0 Answers
How to bake shadows in unity3d free? 1 Answer