- Home /
The question is answered, right answer was accepted
Unable to change Tag of Gameobject using Script
I have gone through all the existing answers for this, including the documentation, but I am still not able to change the tag of the gameobject during runtime. This is the code:
GameObject currTile; void Start () { currTile = gameObject.GetComponent(); currTile.tag = "Unpickable"; //I have also tried putting in gameObject.tag = "Unpickable"; }
Even after this, the tag remains unchanged. Please help me out with this,, thanks!
Answer by game4444 · Sep 18, 2017 at 07:31 AM
Is unity giving any exception? Unpickable Tag is present in your tag list? If don't please add Unpickable in your tag list then try your code.
I got the tags to work by removing the currTile variable itself, and by using gameObject directly. Do you have any idea why it was unable to create a reference to the gameObject?
Thanks for your help
the way of referencing gameObject was wrong. you are writing currTile = gameObject.GetComponent(); you cannot reference gameObject like this. you could write currTile = gameObject; or if you have to reference you could make gameObject public and drag n drop gameObject. But beside all this, if your script is attached on gameObject. You dont need to reference. Directly access gameObject using gameObject.