- Home /
Problem: Adding and Subtracting score
When I call my add or take score function it works but only on the second pass. The first time it is called it doesn't add the score. EG. I run my game, the ball goes out of play for the first time the score is not added or subtracted however the second time, it works correctly added or subtracted.
The problem I had originally was that my playerScore ++/-- was in function update so whenever the ball went out of play it would just keep adding to the score until the ball was within the boundary.
#pragma strict
var hasreset = false;
var cSpeed:float = 10.0;
var sFactor:float = 10.0;
static var playerScore:int = 0;
function Start ()
{
playerScore = 0;
rigidbody.AddForce(Random.Range(-10,10),0,0);
}
function resetball ()
{
yield WaitForSeconds(1.0);
GameObject.FindWithTag("paddle").transform.position.x = 0;
GameObject.FindWithTag("paddle2").transform.position.x = 0;
transform.position.x = 0;
transform.position.y = 0;
hasreset = true;
}
function takescore ()
{
yield WaitForSeconds (1.0);
playerScore --;
}
function addscore ()
{
yield WaitForSeconds (1.0);
playerScore ++;
}
function Update ()
{
//this has something to do with smoothing
var cvel = rigidbody.velocity;
var tvel = cvel.normalized * cSpeed;
rigidbody.velocity = Vector3.Lerp(cvel,tvel,Time.deltaTime * sFactor);
//checking bounds for score and reset
if(transform.position.y <-5.67)
{
resetball();
if(hasreset == true)
{
takescore();
}
hasreset = false;
}
if(transform.position.y >5.67)
{
resetball();
if(hasreset == true)
{
addscore();
}
hasreset = false;
}
}
Answer by whydoidoit · Apr 21, 2013 at 09:38 PM
The first time through hasreset is still false because resetball waits one second before setting it, however the line directly after resetball executes immediately.
You might get what you want by putting all of that in a function:
function ResetAndScore()
{
yield resetball();
if(hasreset)
addScore();
hasreset = false;
}
I fixed it by adding hasreset = true; too the function start(). Thanks for the reply anyway, I realized my wrong doing after you mentioned that hasreset was still false on first pass!