Search Unity

Changing Tags of Multiple objects?

Discussion in 'Editor & General Support' started by Jesse_Pixelsmith, May 23, 2010.

  1. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Is there a way to select several objects in hierarchy and change all of their tags based on your choice in the inspector? It seems to currently only affect the first one clicked...annoying as I have about 60 different objects that I want to change from Untagged to Tag"X".

    Thanks!
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There isn't a way to do it directly from the editor, but you could place the attached editor script in the Editor folder. The menu item comes up under the GameObject menu as Mass Set Tags.
     

    Attached Files:

  3. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    Thank you, andeeee! I had hundreds of objects to tag and was about to hire one of my kids to do it! This is great!
     
  4. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    I can't get this to work in Unity 3, I select all of the objects then when I go to Mass Set Tags, it gives me 2 errors:

    Instance of TagNameWindow couldn't be created because there is no script with that name.
    UnityEditor.EditorWindow:GetWindow(Type)
    TagNameWindow:Init() (at Assets/Editor/tagnamewindow_129.js:6)

    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.EditorWindow.GetWindow (System.Type t, Boolean utility, System.String title, Boolean focus)
    UnityEditor.EditorWindow.GetWindow (System.Type t)
    TagNameWindow.Init () (at Assets/Editor/tagnamewindow_129.js:6)
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You just need to rename the file to TagNameWindow.js. Unfortunately, the forum software sometimes mangles filenames when they are uploaded :-(
     
  6. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    It's still giving me the errors:

    Instance of TagNameWindow couldn't be created because there is no script with that name.
    UnityEditor.EditorWindow:GetWindow(Type)
    TagNameWindow:Init() (at Assets/Editor/tagnamewindow.js:6)

    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.EditorWindow.GetWindow (System.Type t, Boolean utility, System.String title, Boolean focus)
    UnityEditor.EditorWindow.GetWindow (System.Type t)
    TagNameWindow.Init () (at Assets/Editor/tagnamewindow.js:6)
     
  7. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Still having this issue, any fix available?
     
  8. slgooding

    slgooding

    Joined:
    Jan 12, 2009
    Posts:
    112
    As stated earlier, you have to rename the file. Your error shows how you currently have it named in all lowercase. You have the file named as tagnamewindow. Unity is case sensitive. You need to rename the file in unity as TagNameWindow. This is called camel case (FYI) and is pretty standard naming for scripts/code.
     
  9. jbuck

    jbuck

    Joined:
    Jun 9, 2009
    Posts:
    169
    OMG this was helpful. Thank you!

    Can this be easily modified to apply a script to multiple objects?
     
  10. tutibueno

    tutibueno

    Joined:
    Dec 11, 2009
    Posts:
    9
    That was helpful!!

    I´ve changed the code if you want to also change objects layer names:

    Code (csharp):
    1. class TagNameWindow extends EditorWindow {
    2.  
    3.     @MenuItem("GameObject/Mass Set Tags")
    4.     static function Init () {
    5.         // Get existing open window or if none, make a new one:
    6.         var window : TagNameWindow = EditorWindow.GetWindow (TagNameWindow);
    7.         window.Show ();
    8.     }
    9.    
    10.     var tag: String;
    11.     var layer: int;
    12.    
    13.     function OnGUI () {
    14.         tag = EditorGUILayout.TagField("Tag name", tag);
    15.         layer = EditorGUILayout.LayerField("Layer", layer);
    16.  
    17.         if (GUILayout.Button("Apply")) {
    18.             Undo.RegisterSceneUndo("Mass Set Tags");
    19.  
    20.             for (var obj : GameObject in Selection.gameObjects) {
    21.                 obj.tag = tag;
    22.                 obj.layer = layer;
    23.             }
    24.         }
    25.     }
    26. }
     
  11. Chani

    Chani

    Joined:
    Aug 25, 2011
    Posts:
    8
    Superb, just saved me hours of work! Thank you very much andeeee!

     
  12. tutibueno

    tutibueno

    Joined:
    Dec 11, 2009
    Posts:
    9
    Remember that you must put all editor scripts inside Assets/Editor folder. If it doesn't exist, create one and put your script inside there.
     
  13. blitzen

    blitzen

    Joined:
    Sep 4, 2011
    Posts:
    51
    This was useful; thanks Andeeee for the idea and tutibueno for the augmentation. Let's go the whole nine yards by distiguishing layers and tags separately, and applying them to children recursively:
    Code (csharp):
    1.  
    2. class TagNameWindow extends EditorWindow{
    3.     @MenuItem("GameObject/Set Tags and Layers")
    4.     static function Init(){
    5.         var window:TagNameWindow=EditorWindow.GetWindow(TagNameWindow);//Get existing open window or, if none, make a new one.
    6.         window.Show();
    7.     }
    8.     var tag:String;
    9.     var layer:int;
    10.     var tagKindern:boolean=false;
    11.     var layerKindern:boolean=false;
    12.     function OnGUI(){
    13.         var queue:Array;
    14.         var parent:Transform;
    15.         EditorGUIUtility.LookLikeControls(150);
    16.         tag=EditorGUILayout.TagField("Tag name",tag);
    17.         tagKindern=EditorGUILayout.Toggle("Include Children",tagKindern);
    18.         if(GUILayout.Button("Apply Tag")){
    19.             Undo.RegisterSceneUndo("Set Mass Tags");
    20.             for(var obj:GameObject in Selection.gameObjects){
    21.                         obj.tag=tag;
    22.                         if(tagKindern){
    23.                             queue=new Array(obj.transform);
    24.                     while(queue.length){
    25.                         parent=queue.pop();
    26.                         parent.gameObject.tag=tag;
    27.                         for(var child:Transform in parent)queue.push(child);
    28.                     }
    29.                 }
    30.             }
    31.         }
    32.         EditorGUILayout.Space();
    33.         layer=EditorGUILayout.LayerField("Layer",layer);
    34.         layerKindern=EditorGUILayout.Toggle("Include Children",layerKindern);
    35.         if(GUILayout.Button("Apply Layer")){
    36.             Undo.RegisterSceneUndo("Set Mass Layers");
    37.             for(var obj:GameObject in Selection.gameObjects){
    38.                 obj.layer=layer;
    39.                 if(layerKindern){
    40.                     queue=new Array(obj.transform);
    41.                     while(queue.length){
    42.                         parent=queue.pop();
    43.                         parent.gameObject.layer=layer;
    44.                         for(var child:Transform in parent)queue.push(child);
    45.                     }
    46.                 }
    47.             }
    48.         }
    49.     }
    50. }
    51.  
     
    Last edited: Feb 16, 2012
  14. mkingery

    mkingery

    Joined:
    Jan 21, 2011
    Posts:
    14
    these scripts don't seem to work for me.

    i am trying to change a large group of image files to different tags - the tag list that the dialogue window contains is different than the one that unity brings up when you press the blue "..." button.

    when i try any of the options available none of them take, including "untagged" and this is on single selected images.

    thoughts?

    (i am on 3.5.5f3 fwiw)
     
    Last edited: Oct 11, 2012
  15. ezone

    ezone

    Joined:
    Mar 28, 2008
    Posts:
    331
    Here's a C# version of anyone that wants it. Just create a new C# script called 'TagNameWindow.cs' and add this code:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class TagNameWindow: EditorWindow {
    7.    
    8.     [MenuItem("GameObject/Set Tags and Layers")]
    9.  
    10.     static void Init(){
    11.         TagNameWindow window =
    12.             (TagNameWindow)EditorWindow.GetWindow(typeof(TagNameWindow));
    13.         if (window == null) Debug.Log("No window");
    14.     }
    15.  
    16.     string tag;
    17.     int layer;
    18.     bool tagKindern =false;
    19.     bool layerKindern=false;
    20.  
    21.     void OnGUI(){
    22.         ArrayList queue;
    23.         Transform parent;
    24.         EditorGUIUtility.LookLikeControls(150);
    25.         tag=EditorGUILayout.TagField("Tag name",tag);
    26.         tagKindern=EditorGUILayout.Toggle("Include Children",tagKindern);
    27.         if(GUILayout.Button("Apply Tag")){
    28.             Undo.RegisterSceneUndo("Set Mass Tags");
    29.             foreach(GameObject obj in Selection.gameObjects){
    30.                 obj.tag=tag;
    31.                 if(tagKindern){
    32.                     queue=new ArrayList();
    33.                     queue.Add(obj.transform);
    34.                     while(queue.Count>0){
    35.                         parent= (Transform) queue[queue.Count-1];
    36.                         queue.RemoveAt(queue.Count-1);
    37.                         parent.gameObject.tag=tag;
    38.                         foreach(Transform child in parent) queue.Add(child);
    39.                     }
    40.                 }
    41.             }
    42.         }
    43.         EditorGUILayout.Space();
    44.         layer=EditorGUILayout.LayerField("Layer",layer);
    45.         layerKindern=EditorGUILayout.Toggle("Include Children",layerKindern);
    46.         if(GUILayout.Button("Apply Layer")){
    47.             Undo.RegisterSceneUndo("Set Mass Layers");
    48.             foreach(GameObject obj in Selection.gameObjects){
    49.                 obj.layer=layer;
    50.                 if(layerKindern){
    51.                     queue=new ArrayList();
    52.                     queue.Add(obj.transform);
    53.                     while(queue.Count>0){
    54.                         parent= (Transform) queue[queue.Count-1];
    55.                         queue.RemoveAt(queue.Count-1);
    56.                         parent.gameObject.layer=layer;
    57.                         foreach(Transform child in parent)queue.Add(child);
    58.                     }
    59.                 }
    60.             }
    61.         }
    62.     }
    63. }
     
  16. aidesigner

    aidesigner

    Joined:
    May 2, 2012
    Posts:
    121
    There are also a number of tag asset store products that have this function. Full disclosure I own one of them, but it seemed liked thread readers might like options.

    It might also be helpful if any custom implementation would create the tag if necessary. Simply follow this link and you will have yourself a complete custom tag assignment utility.
     
    Last edited: Sep 23, 2017