Disable scripts on all gameobjects with tag

Hi!

I need to disable a script on all my “crate” objects in my scene when the player wins. I’m not sure how to do this so I would love any help! Please post your answer in C#.

Thanks!

The easiest way to do this will be to set a boolean variable up called isEnabled in your crate script, and prevent the script from running if it’s set to false.

   public class ScriptableCrate
    {
         public bool isEnabled = true;
    
         public void Update()
         {
              if (isEnabled == false) // Check if this object is enabled
                   return;
         }
    }

Then, you use this code to set isEnabled on all crates to false:

public void DisableObjectsWithTag(string tagToUse)
{
GameObject[] taggedObjects = GameObject.FindGameObjectsWithTag(tagToUse);
foreach (taggedObject in taggedObjects)
taggedObject.isEnabled = false;
}

GameObject gos = GameObject.FindGameObjectsWithTag(“crate”);
foreach (GameObject go in gos) {
go.GetComponent().enabled = false;
}

‘SomeComponent’ should be replaced by the component name (not in quotes) that you want to disable.