- Home /
counting destoryed objects
hi guys im trying to write a simple bit of code so that once my five game objects have been destroyed i load the next scene but i cnt seem to make anything work
this is what im trying atm a little bit of code so that if it cnt find the game object "Target" which is tagged it load the next scene but this does cause anything to happen
can someone set me on the right lines please function Update() { var Target = GameObject.FindGameObjectWithTag("Target"); if("Target" = null) { Application.LoadLevel (3); }
I would tell you to create a static class holding your counter.
You can find an example here:
http://unitygems.com/memorymanagement/
Go down to static variable to see how it is done. There is also a video at the bottom of the article that shows what is going on. You can also download the project from the video to see how it was done and use the codes.
ok so ive had a go and this is what ive come out with
static var counter;
public void CreateEnemy() { GameObject obj=Instantiate(enemyPrefab, new Vector3(0,0,0).Quaternion.identity);
if(obj)counter++;
}
public void DestroyEnemy() { Destroy(gameObject); counter--; }
if(counter<=0) { Application.LoadLevel(3); }
but its giving me a few errors the main one being unexpected token void
you need to convert to UnityScript. This example is in C#. Remove void for function
Answer by phodges · Oct 28, 2012 at 02:32 PM
In your check before level loading, you are testing against the string "Target" and not the results of your search:
function Update() {
var Target = GameObject.FindGameObjectWithTag("Target");
if(Target == null) { // <--- test against search results, not name.
Application.LoadLevel (3);
}
}
Your answer
Follow this Question
Related Questions
Two cameras and switching 2 Answers
Why doesn't this work? 1 Answer
How can i get a list of tags from the tag manager? 1 Answer
Trigger not registering imported objects 2 Answers
how many tags is too many 3 Answers