Simple question: getting components/functions with multiple objects

Hi all,

I have a simple question and I feel like the answer is right in front of me! I’m trying to grab a group of objects with a specific tag, get a specific script on them, and execute a function for just those objects. Here is what I have:

foreach(GameObject myShapes in GameObject.FindGameObjectsWithTag("tree"))
            {
                prismscript prismScript = myShapes.GetComponent<prismscript>();
                prismScript.Solved();
            }

I’m trying to find objects with that tag and activate the “Solved()” function, but this is only doing it for one of them. How do I make it happen for every object?

EDIT: nevermind, something else was messed up. NEVERMIND!!!

1 Like

One note above, this is fragile inasmuch as it will throw an exception if any “tree” flagged object does NOT have a prismscript on it. If you ever inadvertently flag anything with a tree, your code might break.

To fix that, change the Solved() call to be:

if (prismScript)
{
  prismScript.Solved();
}
1 Like