Search Unity

Why am i getting "Tag not defined" on this code

Discussion in 'Scripting' started by kalle87, Apr 5, 2016.

  1. kalle87

    kalle87

    Joined:
    Mar 3, 2014
    Posts:
    53
    I cant figure out why im getting "Tag not defined" exception on my gameObject.tag = thisID.ToString(); from what ive read this should be the right way to apply a tag to an object manually?

    Code (CSharp):
    1.  
    2.     void Start()
    3.     {
    4.         GameObject aScript = GameObject.Find("DotSpawner");
    5.         scriptSpawnDot myDotSpawnClass = aScript.GetComponent<scriptSpawnDot>();
    6.  
    7.         thisID = myDotSpawnClass.DotID;
    8.         myDotSpawnClass.DotID += 1;
    9.  
    10.         Destroy(gameObject, 3);
    11.  
    12.         gameObject.tag = thisID.ToString(); /// This Line
    13.  
    14.         startLineRenderer();
    15.  
    16.     }
     
    Loveprotooyou likes this.
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Bunny83, lovk4ch and Bram280 like this.
  3. DanHedges

    DanHedges

    Joined:
    Jan 21, 2016
    Posts:
    77
    Snorlax3312 and n4ku like this.
  4. kalle87

    kalle87

    Joined:
    Mar 3, 2014
    Posts:
    53
    God dammit this screws up my whole code then :(
     
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Why don't you just have a class attached to them gameObject's which you use GetComponent on, and give that a string variable called tag? It's a simple workaround :).
     
  6. DanHedges

    DanHedges

    Joined:
    Jan 21, 2016
    Posts:
    77
    @Nigey just so I can understand for my own reference, that would make the code look something like...

    Code (CSharp):
    1. gameObject.myObject.tag = this.id.ToString();
    Is that what you had in mind?

    (obviously not called myObject in real life :) )
     
  7. kalle87

    kalle87

    Joined:
    Mar 3, 2014
    Posts:
    53
    i got it to work, instead of setting the Tag of the gameobject i simply changed the name of it instead and used Find to find it by name :D Onwards to next problem!


    99 little bugs in the code
    99 little bugs in the code
    Take one down, patch it around
    117 little bugs in the code..... Sigh
     
  8. DigitalProgramer

    DigitalProgramer

    Joined:
    Mar 19, 2018
    Posts:
    3
    I had that. just create another tag attach to the object and then put in the code.
     
    mehedihasanp78 likes this.
  9. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    Sorry for digging this up but after all these years there's still no way for native support for an object to have a UUID, GUID, id.. etc
    You have to create workarounds with an extra script to something extremely common.

    (GetInstanceID is not always unique and you can't provide, changing the name is a WORKAROUND)

    I've bumped into this issue on every project hoping Unity would finally fix this
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Bunny83 likes this.
  11. electorchstrauss

    electorchstrauss

    Joined:
    Mar 13, 2021
    Posts:
    11
    if anyone still has this issue and is wondering why, it might be because you need to open Edit -> Project Settings -> Tag and Layers and set it here so unity can reload it and your script find it . If it still doesnt work it might be good to reload the project so the script finds it .