- Home /
How can I make my score counter increase incrementally and not jump from 0 to 100?
So I have a score counter in my game and I would like the counter to increase incrementally. So, for example, If my player shoots and kills his enemy, I want to add "100" points to my score upon the collision, but I dont want my score to jump from 0 to 100, I want it to count up from 0 until it gets to 100 and then stop.
Does that make sense? Thank you so much for anyone who takes the time to read this.
Answer by MacDx · Jan 25, 2018 at 09:58 PM
@PaulSullivan87 If I understand correctly this is what you want. You want to display a score that gets to the target or goal score whenever you get points. Meaning that if you had zero and then pick up something that gives you 5 points then you want to see the UI go, 1,2,3, etc.
Here's how I would do it:
//You need a variable for the actual score
public int score;
//And you also need a variable that holds the increasing score number, let's call it display score
public int displayScore;
//Variable for the UI Text that will show the score
public Text scoreUI;
void Start()
{
//Both scores start at 0
score = 0;
displayScore = 0;
StartCoroutine(ScoreUpdater());
}
private IEnumerator ScoreUpdater()
{
while(true)
{
if(displayScore < score)
{
displayScore ++; //Increment the display score by 1
scoreUI.text = displayScore.ToString(); //Write it to the UI
}
yield return new WaitForSeconds(0.2f); // I used .2 secs but you can update it as fast as you want
}
}
So when ever you increment the score variable, the displayScore variable will update to catch up to score by adding 1 every 0.2 seconds.
Hope this helps!
Thank you so much for taking the time to help me! I will try this ASAP and let you know how it works!
No prob. By the way, LTonon's code, the one he posted in his comment would work too. However, my way of doing it has a big plus which is that you don't have to worry about duplicate coroutines, also you don't have to Start a coroutine every time in every place you want to update the score, since the way I do it, the routine is always working. All you have to do is add to the score and this will just work :P
Yeap, just tested $$anonymous$$acDx's code and it works even though I thought it wouldn't XD (I'm not so familiar with coroutines). Btw, $$anonymous$$acDx, wouldn't a coroutine that runs continuously be more performance consu$$anonymous$$g, since the player won't be incrementing the points all the time? Still, nice to know that your code works, seems a great solution!
Yup don't let the while(true) be misguiding, the routine yields after certain amount of time so it won't freeze, it lets other stuff run while it yields. If there was no yield inside that while(true) then it would indeed freeze (which is a rookie mistake :P and I'd like to think I'm a bit more advanced than that :P).
About performance, the answer is technically a yes, but pragmatically a no. The performance hit there is negligible. Start worrying when you have a thousand of them running at the same time. 20 or 30 (depending on the work you do inside of them of course, in this case we're just incrementing/changing a couple of values) won't make a difference at all.
So I have time today to mess around with this script but I cant seem to get it to work. I'm new to Unity so bear with me. Should this script go on the Text or on another game object? I feel lame for asking for more help, but for some reason the scoring system has really thrown me through a loop.
The way $$anonymous$$acDx did it, I guess the better option would be to assign this script to an empty game object called ScoreController or something like this. After assigning the script there'll be a space on the component (shown in Unity Inspector at the right side of the screen) asking you to give it a Text game object. All you need to do is drag the UI Text component in charge of showing the player score and drop it on the correct field of the Inspector.
Oh ok I see that but I still cant get $$anonymous$$acDx's script to work with my current set up. So I have a script on my score text and a script on my enemy game object. So basically, when my enemy is destroyed, 100 points in added to my score. How do I tell my enemy script that once enemy is destroyed access $$anonymous$$axDx's script?
Answer by LTonon · Jan 25, 2018 at 08:48 PM
If your score is jumping from 0 to 100, it's probably due to your script saying so somewhere that whenever the event you want to cause the score increment happens, the value 100 should be added to the score. Where are you changing the value of your score? It should be something like this, assuming you have a variable called "score" to control it's value (without seeing how you are doing it's hard to tell exactly):
score += 1;
or
score++;
So I haven't implemented the scoring script into my game yet b/c every tutorial in Unity (or on youtube) shows how to increase your score by a specific number of points (+100 points for example).
Could you give me an example of how you're doing it then? Or links of tutorials that you used to do it?
Isn't this tutorial doing exactly what you want? And the "scoring script" is actually done here, I don't get what's the problem you're having...