I have cubes (game objects) instantiating in my level though I want to do a level change only when the all the asteroids are destroyed though some of the asteroid destroy on their own after a timer does anyone have a clue on what I can do and how to do it thank you
save them in an array and then check if its empty
Why does Destroy with timing not work? For example, Destroy(gameObject, destroyTime). If you want to shoot all asteroids, make sure that you control your asteroids’ positions so that you can shoot them; otherwise, they may fly to other galaxies.
You may also Tag your asteroid and then do the following:
var asteroids: GameObject[] = GameObject.FindGameObjectsWithTag ("Asteroid");
if (asteroids.Length > 0)
{
//there is still asteroids
}
else
{
//no more asteroids
}
Note: Usually, a find function is slow. You may not want to call this function every frame.
I was having almost the same kind of problem and found MightyMao’s reply useful. Thanks ![]()
This is how im using the code any ideas where im going wrong
function Update () {
gameObject.tag = "Asteroid";
gameObject.tag = "Asteroid(Clone)";
gameObject.tag = "Asteroid(Clone)(Clone)";
var asteroidsA: GameObject[] = GameObject.FindGameObjectsWithTag ("Asteroid");
var asteroidsB: GameObject[] = GameObject.FindGameObjectsWithTag ("Asteroid(Clone)");
var asteroidsC: GameObject[] = GameObject.FindGameObjectsWithTag ("Asteroid(Clone)(Clone)");
if (asteroidsA.Length >= 0 asteroidsB.Length >= 0 asteroidsC.Length >= 0)
{
Application.LoadLevel("Level_2");
}
}
What’s with the clones. All asteroids should be tagged with just asteroid and saved in a single array
the clones are instantiated from the main asteroid game object as the asteroid objects are destroyed it instantiats into 4 smaller parts of the object shot
Easiest way to keep track of how many asteroids exist is to increment a Counter when you instantiate one, decrease it when its destroyed.
FindObject and related functions per Update() are performance killers.
(In method you were trying you should have only had one .tag of “asteroid”. It’s nothing to do with the prefab name.)
Yea the asteroid clones are still gonna Be tagged as asteroid
Inside a function Awake, you could create Arrays (using GameObject.FindGameObjectsWithTag), that consist of objects tagged with each tag that you’re interested in (one Array per tag).
Then, inside the function Awake still, you define a variable: int, the value of which is the sum of all the aforementioned Arrays’ lengths.
Whenever an asteroid or clone is destroyed, have this var reduced by 1. When it reaches zero it will mean that you will have destroyed everything and you’ll tell the engine to load the next level.
Your following statements assign the gameObject.tag many times. In this case, the last assign statement won.
gameObject.tag = “Asteroid”;
gameObject.tag = “Asteroid(Clone)”;
gameObject.tag = “Asteroid(Clone)(Clone)”;
Therefore, the gameObject.tag now contents “Asteroid(Clone)(Clone)” only. Usually, you create a tag and assign a tag in the Property Inspector.
I suggest that you create a tag or tags in the Property Inspector and assign it to a gameObject in the Property Inspector. You can assign all your asteroid gameObject with the same tag. Unless, you want to uniquely identify them differently. If all your asteroid gameObjects have been assigned with the same tag (Asteroid), you can use the following logic:
function Update ()
{
var asteroids: GameObject[] = GameObject.FindGameObjectsWithTag ("Asteroid");
if (asteroids.Length <= 0)
{
Debug.Log("No more asteriod");
Application.LoadLevel("Level_2");
}
else
{
//check this message in the Unity console
Debug.Log("There are more asteriods");
}
}
Note: You should check for less than or equal (<=) or just (asteroids.Length = 0) instead of >=