- Home /
Destroy Objects to Load New Scene
I have two spheres in my scene. When the player clicks on the 1st sphere, the sphere gets destroyed using the following script:
function OnMouseDown() {
Destroy(gameObject);
}
After the player clicks on the 2nd Sphere, I want a new level to load. I want to use the below script to load a new level when both Spheres are destroyed.
{ Application.LoadLevel("Scene_1"); } }
Any ideas? Thank you!
Answer by Posly · Feb 11, 2012 at 05:27 PM
Make a variable that hold's the number of sphere's in the game, like numberOfSpheres for example. Then everytime you destroy a sphere that value is reduced by one. When "numberOfspheres" is 0, than load the level.
ok I took a stab at it on my own...I tried two methods. Neither has worked. Any thoughts? I am not a programmer so If anyone can edit the existing code I would greatly appreciate it. Thank you.
$$anonymous$$ETHOD 1: var numberOfSpheres = 2
if numberOfSpheres = 0;
{ Application.LoadLevel("Scene_1"); } }
$$anonymous$$ETHOD 2 var numberofSpheres = 2; function LoadLevel () {
if (!numberofSpheres) = 0 {
{ Application.LoadLevel("Scene_2"); } }
}
Look at some basic program$$anonymous$$g syntax and you will notice something is wrong. I can give you a hit; look at you if statements.
Yes, I just did. Look at some basic program$$anonymous$$g syntax and look at your if statements.
Answer by theUndeadEmo · Feb 12, 2012 at 12:37 AM
you could try getting them into an array, you need to tag the spheres to "Sphere"
this is the c# code but you may need to get it to java script
private GameObject[] spheres;
void Update(){
spheres = GameObject.FindGameObjectsWithTag ("Sphere");
if(spheres==null){return; }
if (spheres.Length==0){ //load new level}
}
Thank you for the sample script. I tried this out but also got errors. It get an error for this line of code:
private GameObject[] spheres;
The error is: "A Namespace can only contain types and namespace declarations".
I cannot test out my scene with errors since Unity doesn't allow for it. Thank you though.
Answer by Sharon Hanlon · Feb 11, 2012 at 11:26 PM
Here's one method - you might be able to do it in fewer steps/scripts.
Create an empty game object in your scene and attach the following script:
static var sphereCount : int = 0;
function Update()
{
if (sphereCount == 0)
{
Application.LoadLevel("Scene_2");
}
}
Attach the script below to each of your spheres, then drag the controller you created above into each sphere's Controller property:
var controller : Transform;
function Start()
{
controller.GetComponent(controllerScript).sphereCount++;
}
function OnMouseDown()
{
controller.GetComponent(controllerScript).sphereCount--;
Destroy(gameObject);
}
Your error in the code above is that = assigns the value of 2, while == compares the variable to 2.
Thanks for the script examples. I followed your steps but it is not working. I get a compile error. It says "$$anonymous$$ Identifier: 'Controller Script'." I cannot test the code to see if it works because Unity doesn't let you try your game out with errors.
Whatever you named the first script, is the name that should appear in the GetComponent statements in the second script. I called $$anonymous$$e controllerScript. And make sure you look at each sphere in the inspector and drag the empty game object into the Controller slot.