How can I disable all scripts of Objects, that are tagged? I have Objects tagged as bacteria, they have a script attached. Now I want to disable the scripts attached to this tagged Objects.
At some point during the loop, it’s not finding a component “ScriptName” and you’re trying to disable the script anyways. A simple null-check there would suffice, like:
or you can just disable the whole GameObject with Bac.SetActive(false); A disabled GameObject will have all of its scripts disabled too for the duration. Worth noting that even disabled scripts can still be accessed just fine- it’s only the automated function calls like Start, Update, LateUpdate, etc that won’t fire.
maybe your ScriptName is not a component of your gameobject and it belongs to the children component.
Also your gameobjects must be enable to able to find them,
True about the FindObjectsWithTag only finding active GameObjects, but if it didn’t find a result then the array would be empty and the loop wouldn’t run- no error. The “script it on the child” angle is definitely possible- try GetComponentInChildren() instead of GetComponent() if that’s the case, to return the first instance of the script found on the component or on one of its children. Note that it returns the component attached to the GameObject itself if one exists, and won’t check the children unless the component isn’t found there- the name is a bit misleading in that sense.