systemdebt
systemdebt

Reputation: 4941

Attaching same script to multiple objects

I have a bunch of objects to which I want to attach the same script.Instead of attaching to every game object, I was wondering there is a way like creating a tag for those game objects and attaching that script to all those object?

Upvotes: 0

Views: 1061

Answers (1)

Arturo Volpe
Arturo Volpe

Reputation: 3627

If all gameobjects have the same tag, suppose scriptable, then you could use:

var all = GameObject.FindGameObjectsWithTag("scriptable");
foreach (var go in all)
{
    if (go.GetComponent<ScriptType>() == null)
         go.AddComponent<SCriptType>();

}

This is not the recommended way (you should use prefabs), but works fine.

Upvotes: 2

Related Questions