Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by OgiJr · Dec 24, 2015 at 08:54 AM · realtimechangingtagging

How do you change the tag of an Object in runtime?

Please I am desperate, how do I change a tag during runtime: I tried what I knew from Unity 4- I have a tagged Object "Tag1" and have a second tag "Tag2"; Then I use this: void Update() { gameObject.tag = "Tag2"; } and during runtime it switches between "Tag1" and "Tag2"- but its not possible now:( , help me,please.

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 ZefanS · Dec 24, 2015 at 09:00 AM 0
Share

What exactly is going wrong?

What you're doing is perfectly correct. I just tested it in Unity 5.3.0f4 and it works just fine. Just make sure that you're defined "Tag2" in the Tag $$anonymous$$anager.

4 Replies

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

Answer by mightybob · Dec 25, 2015 at 01:07 AM

If you declare the tag in the tag manager, then

gameObject.tag = "DeclaredTag";

should work fine.

Comment
Add comment · Show 2 · 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 OgiJr · Dec 25, 2015 at 11:58 AM 0
Share

I did find the problem- after a lot of time I realized the problem wasn't in the "gameObject.tag = "Declared Tag";". It was in my system- this is working with if() but not with void On$$anonymous$$ouseDown()- but I really need to do it with if clicked on my object system, any help on that?

avatar image evanwr9 · Nov 10, 2020 at 03:13 PM 0
Share

thank you !

avatar image
-2

Answer by tanoshimi · Dec 24, 2015 at 04:58 PM

You have a typo on line 8. You're assigning the tag "UnitSelected", but testing the tag "UnitSected".

Comment
Add comment · Show 2 · 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 OgiJr · Dec 24, 2015 at 05:14 PM 0
Share

That's not the problem I typed it just now- the system isn't working for my script:/...

avatar image tanoshimi OgiJr · Dec 24, 2015 at 05:31 PM 2
Share

When you're asking for help with faulty code, please don't introduce further errors by then mistyping the code here. Post your actual code, together with any actual error messages.

What does "it will change the tag, but however it is not working it stays tag1" mean? And if it's "not working now", when did it last work?

Please post a screenshot of the tags defined in your project.

avatar image
0

Answer by OgiJr · Dec 24, 2015 at 04:37 PM

The script- using UnityEngine; using System.Collections;

public class Test : MonoBehaviour {

 void OnMouseClick()
 {
     gameObject.tag = "UnitSelected";
 }

 void Update()
 {
     if (gameObject.tag == "UnitSelected") 
     {
         transform.position = new Vector3 (20,5,0);
     }
 }

} I want whenever I click on the object - it will change the tag, but however it is not working it stays tag1 and I am not sure what does defined a tag mean- I mean is it to create a tag?

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 Deadcow_ · Dec 24, 2015 at 11:54 AM

 private void Start()
 {
     tag = "Tag2";
     if (CompareTag("Tag2")) Debug.Log(name + " tag is now Tag2");
 }

Cartainly works, if you declared "Tag2" in the TagManager.

What specifically do you mean by "not possible now"? What error do you have?

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

7 People are following this question.

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

Related Questions

Controlling visibility of several objects with GUI 2 Answers

Changing the direction of an object using another object. 0 Answers

Is it possible to capture each rendered frame without missing frames? 2 Answers

Best Motion Capture Gloves for Realtime use in Unity 0 Answers

How to bake shadows in unity3d free? 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