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?
-
What does the script do?Zach Thacker– Zach Thacker2014-08-15 15:16:46 +00:00Commented 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.htmlZach Thacker– Zach Thacker2014-08-15 16:30:28 +00:00Commented 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 waysystemdebt– systemdebt2014-08-15 18:47:53 +00:00Commented Aug 15, 2014 at 18:47
Add a comment
|
1 Answer
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.