Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Stone-Legion · Sep 08, 2013 at 06:33 AM · taggameobject.tag

C# How to assign tag to GameObject at runtime?

I'm trying to create and assign a tag to a GameObject so I an access it later on, but discovering that I cannot at run time assign tags to GameObjects created.

For example, if I have a loop that creates 100 GameObjects at run time, and I want to give each GameObject an id of 1, 2, 3, 4 all the way to 100. Then, later on, if I want to update object "49" at run time, how can I access this GameObject if I cannot assign a tag to it through C#?

sudo code:

 for(int i = 0; i < 100; i++)
 {
     Create and instantiate 100 game objects;
     object.tag = i.ToString(); //assign object tag
 }
 

Now after, if I want to access one of these gameobjects by the tag, I could just look up the object with "GameObject.FindGameObjectsWithTag()", or so I thought, but this does not work.

How can I create GameObjects in run time, assign them a unique id, and then later access them?

Thanks a bunch to anyone that replies.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dr.Z · Sep 08, 2013 at 06:42 AM 0
Share

You could use Array or List ins$$anonymous$$d of tagging your GameObjects.

Take a look at this post that explains how to create Array and List in C#

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by fafase · Sep 08, 2013 at 06:36 AM

You cannot create tag at runtime, they are defined in the list and you can only choose from one that is there. Your situation is a little sad since you would have to create all possible tag before hand.

A possible solution would be name them appropriately instead and look by name. Tags are more to group object, like "enemy", "projectile" or else. As you are using unique tag, this is not good.

All in all, use name instead and look by name. Note that they all receive a instanceID given by Unity but it is just a number and remembering all of them is...well you know.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
6

Answer by alearso · May 29, 2014 at 09:30 PM

You can do it as long as you define your tag in the "tag list" first. this way you can change back and forth between different tags.

GameObject clone = new GameObject();

clone.gameObject.tag = "myNewTag";

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by amit- · Aug 04, 2018 at 09:13 AM

this.gameobject.tag="newTag";

script must be assign to player or object.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GregoryFenn · Aug 04, 2018 at 01:54 PM

It's a shame you can't just do this ( because GameObject is sealed. Does anyone know why GameObject is sealed? Or a hack-way around it?).

 public class GameObjectWithID : GameObject
 {
         public uint identificationNumber;
         public string idTag {
                 get { return identificationNumber.ToString(); }
         }
 
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect a gameObject's tag via OnCollisionEnter() 5 Answers

Nothing Happens at OnTriggerEnter ? 3 Answers

Array elements change Gameobject.tag 1 Answer

Help with string to gameObject.name 2 Answers

Activate/deactivate all GameObjects with same tag c# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges