Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] How to set a GameObject Tag through a Script?

Discussion in 'Scripting' started by iLoveChocolate, Aug 6, 2010.

Thread Status:
Not open for further replies.
  1. iLoveChocolate

    iLoveChocolate

    Joined:
    Apr 6, 2010
    Posts:
    63
    Could anyone please tell me how to set a GameObject Tag to a 3D Object through a script please?

    I really appreciate your help.
     
    miniturehero and millefoliumink like this.
  2. iLoveChocolate

    iLoveChocolate

    Joined:
    Apr 6, 2010
    Posts:
    63
    GameObject.gameObject.tag="Player";
     
  3. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    gameObject.tag should be enough.
     
  4. iLoveChocolate

    iLoveChocolate

    Joined:
    Apr 6, 2010
    Posts:
    63
    Yeah you are correct! Thanks man! 8)
     
  5. MoMoTaur

    MoMoTaur

    Joined:
    Oct 4, 2015
    Posts:
    1
    Hey, just in case anyone has this problem because I also did, The tag has to be one word.

    gameObject.tag = "Main Camera" is wrong,
    gameObject.tag = "MainCamera" is right
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    no, tags can have spaces.

    If you are trying to use the functionality of
    Code (csharp):
    1.  
    2. Camera.Main.
    3.  
    you just need to use the "correct" spelling for that function, i.e.

    http://docs.unity3d.com/ScriptReference/Camera-main.html
     
    Kiwasi likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Tags should also be set up in the tag manager before use.
     
  8. AbdulRaman

    AbdulRaman

    Joined:
    Sep 5, 2017
    Posts:
    1
    is there any way to give a GameObject a Tag through a Script without setting the tag up in the tag manager before Scripting it?
     
  9. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    You have to set a tag in the tag manager beforehand and then, in the script, you can assign it (for example, if you take a key you can change the tag of doors from "Closed" to "Open", as long as the "Open" tag exists in the tag manager).
    As far as I know, you can't create a tag at runtime.
     
    Last edited: Sep 16, 2019
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    At editor time you can use an editor script to create a new tag.

    At runtime you are better off implementing your own tagging system if you need to create new tags.
     
    CyrilGhys, NeatWolf and SparrowGS like this.
  11. Deleted User

    Deleted User

    Guest

    Hello,
    Can anyone help me with this script? i want to tag more gameobjects with this script ,but i don't know how to do it.
    Any help would be appreciated

    The script (javascript):

    if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    {
    if(hit.collider.gameObject.tag == "Door4v2") // here i want to tag another gameobject
    {
    guiShow = true;
    if(Input.GetKeyDown("e") && isOpen == false)
    {
     
  12. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    i have a game object. clicking it should change its tag to another. pls help!!!!
     
  13. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Unity tagging system is very poorly designed. Use it where appopriate, but if you want to heavily rely on tags in your game consider buying something better from asset store or writing your own.
     
  14. DBarlok

    DBarlok

    Joined:
    Apr 24, 2013
    Posts:
    268
    Why? Im using TAGS since ages and not even a problem and im relying heavily on them. Please explain why Unity Tags are so bad designed, because in here it works.
     
    Last edited: Sep 16, 2019
  15. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    String comparisons.
    Say you decide to change or remove a tag; now you must sift through every reference in every script in your project and change or remove the references from those places as well.
    The best you could do to prevent this is to create a global static class holding constant string values of each Unity tag, and reference these constants instead of typing in tag names directly, but you would still need to manually edit this class when changing tags and other classes as well when removing tags.

    If editing Unity tags compiled an enum during edit-time containing string literals of each tag you specify, I'd likely be using them more.
     
    DBarlok likes this.
  16. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Ever needed to place two different tags onto the same GameObject?
     
    Dextozz and DBarlok like this.
  17. DBarlok

    DBarlok

    Joined:
    Apr 24, 2013
    Posts:
    268
    @Vryken Can i just open an IDE and put Search And Replace? For example, tag: let's put "Ugly_Orc" tag here.
    Then i go to change the tag 3 months later (why?). If i change the tag name i will never know
    why the OnTrigger function that it is looking (sorry my english again) for this tag, it is working.

    So, in the first place, i will try to not change the tag. But if i need to, then Search And Replace
    will be my friend. But i work just on 1 game and sporadic apps, so, i dont know in wich environment
    this will be a heavy problem. Of course if the Editor just tell me with a Popup: If you change this
    tag name all your references inside all your scripts will be lost, are you sure? I see the point
    about this as bad designed now. So i agree about it is risky and needs an improvement.

    Oh god, never change the name of the tag. Will instantly kill you.

    @Kiwasi No. WHY? ! haha. But maybe yes. I had to use the GameObject name as "tag" i think (because i cannot have 2 tags in same GameObject scenario) and compare against the GameObject name and the Tag, or just put a child with another Tag.
    It's tag maze.
     
    Last edited: Sep 20, 2019
    NeatWolf likes this.
  18. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Looks like already are familiar with the limitations of tags.
     
    DBarlok likes this.
  19. samf1111

    samf1111

    Joined:
    Sep 18, 2019
    Posts:
    16
    these limitations are probably caused by unity's compiler rendering tags as an integer, to be used as an index in a list of tags controlled by the tag manager. this is useless, because no one is going around making 200 character tags anyways. but i'm still just fine using tags.
     
  20. efoue2

    efoue2

    Joined:
    Jul 23, 2020
    Posts:
    14
    If my game object was called Custom_player Current How would I do this?
    (I want to add a tag "player" to my player so I can have zones where certain sound plays)
    thx if you answer!
     
  21. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Is it called "Current", or "Custom_player"? Or is it not a GameObject but a class called Custom_player?
     
  22. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You'd just set the tag as usual.
    The name of the GameObject makes no difference.
     
  23. efoue2

    efoue2

    Joined:
    Jul 23, 2020
    Posts:
    14
    actually nvm i fixed it but thx!\
     
  24. astechnolabs

    astechnolabs

    Joined:
    Jul 11, 2021
    Posts:
    24
    can Anyone guide me?
    how to create new tag from script .cs file?
     
  25. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
Thread Status:
Not open for further replies.