Hi,
I’m testing a score counter script. I have a box that will instantiate a sphere each time I click LMB. the sphere will score +1 if it hit the plane below. I gave 3 counts for player to hit the plane via inspector, and when the count 0, a function will see how many score. If 0 ( no hit the plane ) then it will load a scene named “lose”, and if hit the plane more than 1, it will load “win” scene.
So far it worked, but I hit a strange problem. If the first ball hit the plane, then no matter the rest, it will load “win” scene, just as expected. But if the first and second ball missed the plane, then the third ball hit the plane, it will load “lose” scene, as if it omits the last ball hit. What’s wrong?
btw, “count” means how many balls left and “score” means how many time the ball hit the plane…
Again, the script works well, but if first and second ball missed and the third ball hit the plane, then why it loads “lose” scene even the score is 1 ( hit the plane )?
because load the “lose” scene should be only if the score is 0.
Solved! Simply add WaitForSeconds in the function EndGame ! Thanks to all who replied!
here’s the script attached to the plane:
var score : int;
var count : int;
var bola : Rigidbody;
function OnCollisionEnter (col : Collision) {
if(col.gameObject.tag == "ball"){
score ++;
Destroy (col.gameObject);
}
}
function OnGUI(){
GUI.Label(Rect(50,50,100,100), "score" +score);
GUI.Label(Rect(50,150,100,100), "count" +count);
}
function Counting(){
count --;
if(count == 0){
EndGame();
}
}
function EndGame(){
if(score == 0){
yield WaitForSeconds (2);
Application.LoadLevel ("lose");
}
if(score >=1){
yield WaitForSeconds (2);
Application.LoadLevel ("win");
}
}
and this is the script for the box
var bola : Rigidbody;
var speed : float = 10.0;
var rotationSpeed : float = 100.0;
function Update () {
if(Input.GetMouseButtonDown(0)){
var clone : Rigidbody;
clone = Instantiate(bola, transform.position, transform.rotation);
gameObject.Find("plane").SendMessage("Counting");
}
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
}