How to add one score every second to scoremanager c#
Happy new year!
I got question regarding time and score. How do I add one score every second to my scoremanager. I got button which stops the time when pressed. After the button is being pressed I would like to see time as score (int) so that I can save it to my scoreboard. Here is my timer script.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class TimerTimer: MonoBehaviour {
public Text counterText;
public float seconds, minutes;
void Start (){
counterText = GetComponent<Text> () as Text;
}
void Update(){
minutes = (int)(Time.time/60f);
seconds = (int)(Time.time % 60f);
counterText.text = minutes.ToString("0") + ":" + seconds.ToString("0");
}
}
Answer by TooLate · Dec 31, 2016 at 10:05 PM
I searched around more and figured out that I need separate code to do this. I tried this but invoke method could´t be called. Any advice?
using UnityEngine; using System.Collections.Generic;
public class ScoreSecond : MonoBehaviour {
private int now = 3;
private int score = 0;
public GUIText scoreLabel;
void Start(){
scoreLabel.text = score.ToString();
}
void Update(){
InvokeRepeating ("AddToScore", 01f, 02f);
}
void AdToScore(){
if (now > 0) {
score = score + 1;
scoreLabel.text = score.ToString();
}
}
}
01f is garbage, you want to say 'InvokeRepeating ("AddToScore", 1.0f, 1.0f);'
Also you misspelled your function name,` void AdToScore()` ins$$anonymous$$d of void AddToScore()
but you called the correctly spelled name (AddToScore) in the invoke. Since you have no function called AddToScore, it will not even try to work.
I made the changes and the code works. Thank you $$anonymous$$r. Only problem is that the it dont give one point/second. If I wait for example 3 seconds the Guitext counter show 148. Any suggestions how to fix this?
I found a way to do it. It works almost perfectly. There is just a slight delay at the beginning. Cheers!
using UnityEngine; using System.Collections;
public class ScorePoint : $$anonymous$$onoBehaviour {
private IEnumerator coroutine;
void Start()
{
coroutine = WaitAndPrint(1.0f);
StartCoroutine(coroutine);
}
private IEnumerator WaitAndPrint(float waitTime) {
while (true) {
yield return new WaitForSeconds(waitTime);
Score$$anonymous$$anager.score += 1;
}
}
}
Your answer
Follow this Question
Related Questions
reload game 1 Answer
How do I add a score system? 1 Answer
Looking for a simple Score Multiplier. 1 Answer
Problem with counting score in 3D game 1 Answer
How do I make the Score system work? 1 Answer