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.
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.
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.