2
\$\begingroup\$

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.

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

2 Answers 2

4
\$\begingroup\$

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.
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Also, if the objects are instantiated from a (or multiple) prefab(s), the change can be made to the prefab(s) and then applied. \$\endgroup\$
    – nickson104
    May 26, 2015 at 12:37
  • \$\begingroup\$ Yet to learn about prefabs. Just started. \$\endgroup\$ May 26, 2015 at 12:58
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Latency
    May 26, 2015 at 21:47
2
\$\begingroup\$

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");"
}
\$\endgroup\$
1
  • \$\begingroup\$ 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). \$\endgroup\$
    – Kroltan
    May 26, 2015 at 15:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .