How to add scripts/components to many gameobjects at the same time?

I have a scene with 116 small objects that all use the same script, GUISkin and so on. Now I have to replace my placeholders with the real objects and I have to reapply two scripts and a GUISkin on all objects, which comes to 348 things to add.

Please tell me there’s a way to apply the same script to multiple objects at a time. I’ve looked in Unity, in the manual and on the forums, but I can’t find anything, I’m just hoping that I suck at searching.

Unity itself doesn’t have that functionallity (what a shame, huh?)

Fortunatelly, the guys at Unity Team made an amazing job while making the Editor classes, so we can extend Unity to our needs.

Use this script, save it as “MultiScript.cs” in a folder named “Editor” in your Assets folder:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class MultiScript : EditorWindow
{
    private MonoScript s;
    private string c;
    private bool addToChildren = false;

    [MenuItem("Window/MultiScript")]
    static void Init()
    {
        MultiScript window = (MultiScript)EditorWindow.GetWindow(typeof(MultiScript));
    }

    void OnGUI()
    {
        s = EditorGUILayout.ObjectField("Script to add", s, typeof(MonoScript)) as MonoScript;

        c = "";
        if (Selection.gameObjects.Length > 0)
        {
            for (int i = 0; i < Mathf.Min(10, Selection.gameObjects.Length); i++)
            {
                c += Selection.gameObjects[i].name + "\n";
            }

            if (Selection.gameObjects.Length > 10)
                c += "(" + (Selection.gameObjects.Length - 10) + " more objects)";
        }

        if (s != null)
        {
            addToChildren = GUILayout.Toggle(addToChildren, "Add to all children (recursive add)");
            GUILayout.Label("Objects to receive " + s.name + ":\n\n" + c);
            if (GUILayout.Button("Add " + s.name + " to all " + Selection.gameObjects.Length + " objects")) addScriptToObjects();
        }
        else
            GUILayout.Label("Select a script first");
    }

    void OnInspectorUpdate()
    {
        Repaint();
    }

    private void addScriptToObjects()
    {
        if (Selection.gameObjects.Length > 0)
        {
            foreach (GameObject o in Selection.gameObjects)
            {
                o.AddComponent(s.name);

                if (addToChildren)
                {
                    recursiveAdd(o);
                }
            }
        }
    }

    private void recursiveAdd(GameObject o)
    {
        foreach (Transform t in o.transform)
        {
            t.gameObject.AddComponent(s.name);

            if (t.childCount > 0)
                recursiveAdd(t.gameObject);
        }
    }
}

It’s really simple to use, go to Window → MultiScript.
Select the objects you want to have a script.
If they have children, and you want them to also have that script, check the option.
Select the script you want, and hit the button.

Of course, make a backup before.

Hope it helps!

That works like a charm! Thank you very much, you just saved me a lot of time and frustration.

Just a quick question: I get the error “Assets/editor/MultiScript.cs(14,21): warning CS0219: The variable `window’ is assigned but its value is never used”. The script works, but the error hides other “real” errors and is a bit in the way. Do you know what’s wrong?

Oops, disregard the question. After minimizing and maximizing the Unity window, the error disappeared.

Thanks again for the script!

Don’t worry, it’s just a warning. I’ve copy-pasted the script for showing the window from the docs, lol.

Glad it helped =)

Wow, thank you for putting this out there, saved me a TON of time as well.