destroy tagged Object(s) and loading a level problem

Hi there,

i´m not a good Coder so excuse me my little Question (and my bad english!)

I´ve modified the FPS Tutorial into a SciFi ShootEmUp Game. The Basics are working fine for me.
A small testing Level is finished.

Now i have a little Problem and hope someone can help me out.

There are two Buildings in my level that are the primary targets to destroy.
Both have the the TAG “target” assigned to them.
If they are destroyed i want to call a function, in this case loading a new Level.
I´ve created a empty game Object and assigned the following script to it.

function Update () {

if ((GameObject.FindGameObjectsWithTag ("target")) == 0);

Application.LoadLevel ("Intro");

}

What happens is that as soon as the Game starts the Level “Intro” ist loaded.
The Objects are not destroyed at this time.

Whats wrong with this ?

Or is there a better way to define a “Misson Complete” status after destroying the targets ?

Thanks in advance.

Hiya - few comments on your approach here.

  1. Update() gets called once a frame. You are doing relatively expensive work by doing your polling here once a frame, and also relying on a string-find within the game world. I personally prefer game classes that have a standing knowledge of the key objects in a given scene (i.e., a GameObject[ ] array). So for example if you had a Coroutine or something going on you could do this polling / state-checking once every 2 seconds (instead of 60 times in an Update loop when your stuff is cranking out 30fps). Once the “target array” is empty, update the current game state.

  2. The reason why your snip is not working as expected is because the return type (GameObject[ ]) will never equal the integer 0. It’s the length property of the array you need to compare.

To easiest and fastest way to dynamically keep track of destroyed objects is an dummy gameobject with the following script:

#pragma strict
public var watchObjects : GameObject[];
private var finished : boolean = false;
private var remainingObjects : boolean;
function Update() {
 if (!finished) {
  if ((Time.frameCount % 10) == 0) { // test every 10th frame
   if (watchObjects.length>0) {
    remainingObjects = false;
    for (var i : int = 0; i<watchObjects.length; i++) {
     if (watchObjects[i]) {
      remainingObjects = true;
      break; // early out 
     }
    }
    if (remainingObjects == false) {
     finished = true;
     Application.LoadLevel(xxx);
    } 
   }
  }
 }
}

Just set size for watchObjects in the inspector and drag the gameobject you want to watch.

Thank you very much, this is a great solution.
It works fine for me.

thanks, thanks, thanks…