0

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?

3
  • What does the script do? Commented Aug 15, 2014 at 15:16
  • That isn't very informative :P but I think I get the idea. Are you using prefabs? docs.unity3d.com/Manual/Prefabs.html Commented Aug 15, 2014 at 16:30
  • No, 6 game objects as part of the track and one they pass by, then repeating by setting the position ahead of what is visible.I know that is not the best way to do it but it has been handed over to me this way Commented Aug 15, 2014 at 18:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.