- Home /
How to Update a score count going up and down
Hi I'm trying to make it so that when a ball hits the ground, the score count goes down by 1.
I already have it where if the ball hits a moving box, then the score count goes up by 1, which is fine.
But with my code right now, if my score count is at lets say, 30. And if the ball hits the ground, I'd want it to go down to 29. But right now if the ball hits the ground, it starts at 0 and goes down to -1. And when the ball hits the box it goes back to the previous score of 30 and bumps it up to 31.
Any help would be appreciated, thanks!
public GameObject ball;
public Text countText;
public float volume;
public AudioClip SoundToPlay;
AudioSource audio;
private int count;
void Start()
{
count = 0;
SetCountText ();
audio = GetComponent<AudioSource> ();
}
void OnTriggerEnter2D (Collider2D paddleTrigger)
{
if (paddleTrigger.CompareTag ("ball"))
{
count = count + 1;
SetCountText ();
audio.PlayOneShot (SoundToPlay, volume);
}
}
void SetCountText()
{
countText.text = "" + count.ToString ();
}
//This script ABOVE ^^^^^^^^^^ is for the count to go UP when hitting the box (paddle)
//This script BELOW vvvvvvvvvv is for the count to go DOWN when hitting the ground
public GameObject ball;
public Text countText;
public float volume;
public AudioClip SoundToPlay;
AudioSource audio;
private int count;
void Start()
{
audio = GetComponent<AudioSource> ();
this.GetComponent<BoxCollider2D> ().enabled = false;
}
void UpdateCount()
{
SetCountText ();
}
void OnTriggerEnter2D (Collider2D groundTrigger)
{
if (groundTrigger.CompareTag ("ball"))
{
count = count - 1;
SetCountText ();
audio.PlayOneShot (SoundToPlay, volume);
}
}
void SetCountText()
{
countText.text = count.ToString ("");
}
}
Answer by cstooch · Jun 23, 2017 at 05:54 AM
First off, these are two different scripts, right? (Yes, they would be, but I should ask in case I'm having a brain fart here.. lol)
What I'm seeing here that's a problem..
It looks like you have two separate count variables, and that's why they are giving you weird results when they hit the different colliders. You need to declare this variable only once, and somewhere where both scripts would have access to this count, for example a "Game Manager" singleton, or something like that, or even in a script that is on the object that is parent to these other two scripts. Then within this central area where both scripts are able to access the variable, you'd declare it like so: public static int count = 0; There's a lot of info out there on how to access variables in other scripts.
Just as a tip.. us coders can be lazy at times. count = count - 1 and count = count + 1 are too verbose for us lazy folks.
You can actually replace these with:
count++;
count--;
......
count = count - 1;
// the above and below do the same thing
count--;
Next, here's what you'd want to do to prevent decrementing below 0:
if (i > 0) i--;
Yeah they are both separate scripts, haha.
You're response totally makes sense and I got it to work! Basically just made that top script declare the variables and functions and then called them in the 2nd script.
Thanks a lot!
Yeah, that definitely would work. No problem!
Your answer
Follow this Question
Related Questions
Update() doesn't update so well 1 Answer
Enemy spawner help with lengths 0 Answers
Getting function from other script? 3 Answers