How do I…
like
say I wanted to assign the gameobject to “Tag_1”
Like how would I do that… like… something along the lines of this?
Assign.Tag(gameobject,“Tag_1”);
How do I…
like
say I wanted to assign the gameobject to “Tag_1”
Like how would I do that… like… something along the lines of this?
Assign.Tag(gameobject,“Tag_1”);
gameObject.tag = "Tag_1";
The tag must already be declared in the Tag Manager. Tags cannot be created at runtime.
o… so what would above do… rename it?
Sure, that could work. Just drop this script in the project folder and that code will be as good as gold:
using UnityEngine;
public class Assign {
public void Tag(GameObject target, string newTag) {
target.tag = newTag;
}
}
Oh another way I like to increase tag security is by declaring all tags in one script, like this:
using UnityEngine;
public class Tags {
public const string Untagged = "Untagged";
public const string Player = "Player";
}
That severely decreases the likelihood of programmer error due to the fact that you leveraging the power of the compiler to make sure that it’s the same wherever it’s needed. You would use it like this:
// good way to assign tags
gameObject.tag = Tags.Player;
// good way to check for tags
if (gameObject.CompareTag(Tags.Enemy)) { }
Nope, it would throw an exception.
SO DanielQuick was wrong and you CAN make tags in runtime?
Tags are really easy to use. You should really take the time to read the docs.
And if you can’t find it, there are a lot of posts about tags in general.
Here are both links about tags in general
http://docs.unity3d.com/Documentation/Components/class-TagManager.html
http://docs.unity3d.com/Documentation/Components/Tags.html