Let’s say that i have couple boxes which has been randomly moved by gravity, and at the end of the level i don’t need them anymore (later i will instantiate another as a next lvl). Is there any way to destroy all of them with a single line of code for example by tag? Another question: is there any way to check for rigidbody.IsSleeping() of every object in my scene with a single tag? right now i must to write every object in script and test: if level 1 so check if those object sleeping, if level 2 (where i have more objects) - check if all lvl2 obj are sleeping etc.
Objects that haven’t been marked as ‘don’t destroy on load’ will be cleaned up automatically when a new scene is loaded, so I think the first step would to make sure that there’s really a need to destroy the objects you mentioned.
But, the function you’re looking for is most likely GameObject.FindGameObjectsWithTag(). Code to destroy all objects with a certain tag might look like this (C#, not compiled or tested):
public static void DestroyGameObjectsWithTag(string tag)
{
GameObject[] gameObjects = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject target in gameObjects) {
GameObject.Destroy(target);
}
}
(No guarantee that’s exactly correct, but it should be something like that.)
You can use a similar method to check if the rigid bodies associated with all game objects with a certain tag are asleep.
Thanks for reply. I forgot to mention that i would like to avoid loading every time new scene, coz my levels are very fast to solve and that would cause repeating soundtrack from the beginning over and over. I have tried to use FindGameObjectsWithTag before but no success. My scripts are in JS and it’s all i understand so i would like to avoid C#. Anyway i will try to look closer to your post…maybe i will work something out. Thanks
If you’re having problems using FindGameObjectsWithTag(), post the code you’re having trouble with, along with a description of the problem. (Whatever the problem is, I’m sure someone will be able to help you get it sorted.)
Hey guys Thanks for your help. But i am facing another problem I making 2D game. I want to destroy 2 gameobject when they overlap and also player tap on them. So I don’t know how to do that. Kindly help me. I am searching from 1 month for this solution.
Hey ;D You can simply add Colliders to your 2 gameobjects and your Player and define them as trigger. Give Ur GameObjects and Player Tags to identifie them and use a Script with the Methode OnTriggerEnter(Collider other) { } to Destroy or Disable the Obejcts u want to expire