Hello. I’m working on a game in which there are 2 ways of scoring. 1- 1point/sec while holding a ball. 2- 5 points if you drop the ball in a basket. I have multiple questions in this but I’d like to focus on the 1pt/sec part for now.
Here’s my code (sorry it’s pretty sloppy, I’m kind of new):
public int score;
private bool hasBall;
private float startTime;
void Start()
{
score = 0;
hasBall = false;
startTime = Time.time;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "ball")
{
//Destroy(other.gameObject);
other.gameObject.transform.parent = gameObject.transform;
hasBall = true;
}
else
hasBall = false;
}
void Update()
{
if (Input.GetKeyDown("space"))
print("throwBall");
if(hasBall)
score = (int)(Time.time - startTime);
Debug.Log("Score: " + score);
}
This works for giving me 1 point per second, but it doesn’t recognize when I pick it up as the start time. My debug log often will say I have “Score: 5” the first second I pick it up.