• 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
0
Question by Jdogmaster · Apr 12, 2018 at 11:54 PM · tagtagsreleasetaggingpull

Making object switch between two tags every 3 seconds

how Can I make a script that makes an object switch between 2 tags every 3 seconds?

Comment
Add comment
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

3 Replies

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

Answer by MarioSantoso · Apr 14, 2018 at 12:10 AM

Assign this script and play, you should have what you want, don't forget to change interval.

 using UnityEngine;
 
 public class ChangeTag : MonoBehaviour
 {
     public float interval;
     public string[] tags;
 
     private float _timer;
     private int _tagID = 0;
 
     void Update()
     {
         if (_timer >= interval)
         {
             _timer = 0;
             gameObject.tag = tags[_tagID];
             _tagID++;
             if (_tagID > tags.Length)
                 _tagID = 0;
         }
         else
             _timer += Time.deltaTime;
     }
 }
 
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 shadowpuppet · Apr 13, 2018 at 06:24 PM

Since no one has answered I'll take a stab at it. May not be the best answer but at the moment it is the only answer. I would use a coroutine.

 using UnityEngine;
 using System.Collections;
 
 public class testSwap : MonoBehaviour {
 
     public bool changed;
     void Start() {
         changed = false;
         Debug.Log ("start");
         
         StartCoroutine(ChangeTag());
     }
     
     private IEnumerator ChangeTag(){
         yield return new WaitForSeconds(3);
         if (gameObject.tag == "tag2"&& changed == false) {//one of your tags to toggle
             gameObject.tag = "tag1"; //the other tag you want to toggle
             changed = true;
             Debug.Log ("now tag1");
 
         }
         
         if (gameObject.tag == "tag1" && changed == false) {
             gameObject.tag = "tag2";
             Debug.Log ("now tag2");
             changed = true;
 
         }
         Start ();
         yield return null;
     }
 }
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 Bunny83 · Apr 13, 2018 at 07:32 PM 1
Share

Extremely bad solution because:

  • you should never call Start manually. In general you shouldn't call any Unity callback manually. It's bad practise in general.

  • What's the point of the boolean since you already check the tag?

  • The yield at the end has absolutely no effect beside keeping the coroutine one more frame alive.

  • Calling a coroutine recursively is in general a bad idea. Starting a coroutine generates garbage and has some overhead. Just use a loop.

avatar image shadowpuppet Bunny83 · Apr 13, 2018 at 09:18 PM 0
Share

for some reason a lot of times if I don't do the boolean things happen twice. Especially on cases of instantiating a ragdoll after death. I get two rags or even three unless I put a if dead false bool followed by setting it to true.

avatar image
3

Answer by Bunny83 · Apr 13, 2018 at 07:35 PM

The easiest and most straight forward solution would be using a coroutine like this:

 void Start()
 {
     StartCoroutine(ChangeTag());
 }
 
 IEnumerator ChangeTag()
 {
     var wait = new WaitForSeconds(3);
     while (true)
     {
         yield return wait;
         gameObject.tag = "tag1"; 
         yield return wait;
         gameObject.tag = "tag2"; 
     }
 }

Comment
Add comment · Show 1 · 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 Tubestorm · May 22, 2020 at 03:40 PM 0
Share

doesn't swap continously.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

79 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 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 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 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 avatar image avatar image avatar image

Related Questions

Destroying Multiple Objects with Tags 1 Answer

Match 3 game getting the colour of the match 1 Answer

nullReferenceException when comparing tag in a search for a specific parent. help. 1 Answer

Raycast isn't working when with tag 2 Answers

Delete some unused tags and game objects tag changes unity 5 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges