# How to apply a script to multiple Objects in Unity?

In Unity, I've almost 200 or more objects. I wish to apply single script to all those objects.

I'm a beginner & learning scripts in Unity.

Edit: At the time of editing the scene itself.

• Are you talking during runtime, or simply in the editor? You have not clarified, this is important. The assumption is you mean at runtime May 26, 2015 at 11:39

## 2 Answers

Here are the steps for applying scripts to multiple objects.

• Step 1. Select all your objects from Hierarchy panel.
• Step 2. Then from Project panel drag & drop your script into Inspector panel.
• Also, if the objects are instantiated from a (or multiple) prefab(s), the change can be made to the prefab(s) and then applied. May 26, 2015 at 12:37
• Yet to learn about prefabs. Just started. May 26, 2015 at 12:58
• You should take a look at prefabs, before you get to deep into your game, they are easy to pick up and will save you a ton of time. A quick run down would be, if you have an item that you want to have multiple copies of, drag it into the project panel, each time you drag that item that was created into the hierarchy panel it will create a new instance of it. If you make changes to the item and apply them it will carry over across all of those objects. May 26, 2015 at 21:47

You can add script at runtime. You can create an empty objects with a script that "browse" the 200 or more objects and atach the sript to each of them

AtachScriptAtRunTime

Example: to add the script named FoobarScript to the game object use:

gameObject.AddComponent ("FoobarScript");


Clearly you must recognize all the objects. (example you can use FindGameObjectsWithTag) Full example:

objList = GameObject.FindGameObjectsWithTag("MeaningfullTagName");
foreach (GameObject obj in objList ) {
obj.AddComponent ("YourScript");"
}

• Please include the explanation in the answer, in case your links go down (I know it's the official U3D forums, but still, less clicks for readers). May 26, 2015 at 15:18