Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
Are you talking during runtime, or simply in the editor? You have not clarified, this is important. The assumption is you mean at runtime –  nickson104 May 26 at 11:39

2 Answers 2

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.
share|improve this answer
2  
Also, if the objects are instantiated from a (or multiple) prefab(s), the change can be made to the prefab(s) and then applied. –  nickson104 May 26 at 12:37
    
Yet to learn about prefabs. Just started. –  Sagar R. Kothari May 26 at 12:58
1  
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. –  Latency May 26 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");"
}
share|improve this answer
    
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). –  Kroltan May 26 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.