- Home /
Changing scene when all the object is destroyed
so here, let say i have 3 gameobject(cubes) with different names, i did use Destroy(gameObject) and its fine. but my problem is, im having a trouble making script that if i clicked the gameObject / Destroyed. it will change the scene/load level. any answer would be much appreciated.
You meant to change the scene when all the three objects you mentioned get destroyed..?So you need to detect whether the 3 objects are destroyed or not..?
@Bojidar : Post it as an answer, so I can't vote for you. :)
There is a little button on the left, so you can vote comments up (no button to vote down).
Answer by Bojidar · Jul 29, 2014 at 12:58 PM
Use GameObject.Find("name1") and GameObject.Find("name2") when you Destroy "name3".. If both return null, then you must load the scene/level. Same goes for the other two cubes.
Answer by dsada · Jul 29, 2014 at 01:14 PM
EDITED:
You can put a script on each cube that has the following in it:
public class DestroyDetection : MonoBehaviour
{
private static int objectsNeedToBeDestroyed = 0;
private static int objectsDestroyed = 0;
void Start()
{
++objectsNeedToBeDestroyed;
}
void OnDestroy() //will be called on destroying the object
{
if(++objectsDestroyed == objectsNeedToBeDestroyed)
{
Application.LoadLevel("YourLevelName");
}
}
}
Note that this hasnt got an Update function that makes this optimal
All three objects must be destroyed before you change the scene ($$anonymous$$aybe use a statc class?).
Answer by Paulo33 · Jul 29, 2014 at 01:34 PM
Super Simple way...
var objectsCollider: Collider;
var objectsCount: int = 0;
function Update(){
if(objectsCount >= 3){
application.loadlevel("Blablabla");
}
}
function OnColisionEnter(objectsCollider:Collider){
objectsCount++
}
Answer by sethuraj · Jul 29, 2014 at 02:12 PM
Another simple way of doing this is to keep the three cubes as a child of an EmptyGameObject and check against if the child count is zero or not.Simple...Like this
void Update()
{
//If there are no child (which are the cubes) then change the scene
if(transform.childCount<1)
{
Application.LoadLevel("YourLevelName");
}
}
Your answer
Follow this Question
Related Questions
Input wont work!!! 0 Answers
When using input, array.Add replaces the last member 1 Answer
Adding instantiated objects to arrays 1 Answer
loading a folder of textures to an array 0 Answers