- Home /
C# How Add new score to old score in update function
I have been struggling to get my head round to adding my new score to the old score in update function with Mathf.move towards but not work! score increase to infinity, I use bool, while, limited time nothing work please help me, I want to add remaining time left + current score.
void Start() { LevelNumber = SceneManager.GetActiveScene().buildIndex;
timertext.text = "00:" + secondLeft;
nextTime = Time.time + delay + 2;
VictoryPanel.SetActive(false);
GameOverPanel.SetActive(false);
cam = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<CameraManager>();
//cam.Default();
go = false;
CountDownAnimimator.SetBool("countdown",true);
currentScore = PlayerPrefs.GetFloat("score");
//PlayerPrefs.DeleteAll();
}
void Update()
{
scoretext.text = "Score: " + currentScore.ToString("0");
if(timeleft <= 0)
{
timeleft = 0;
cam.ChangeToEnd();
//Time.TimeScale = 0;
//timeisUp.gameObject.SetActive(true);
//Restartbutton.gameObject.SetActive(true);
}
timertext.text = "" + Mathf.Round(timeleft);
if (Time.time >= nextTime)
{
StartCoroutine(SetCollor());
nextTime += interval;
}
if(timeleft > 0 && Time.time >= nextTime -1)
{
if(Timer)
{
timeleft -= Time.deltaTime;
go = true;
}
if(timeleft <= 10)
{
timertext.color = Red;
}
if(title1.GetComponent<SpriteRenderer>().color == Green )
{
cam.ChangeToEnd();
Timer = false;
timertext.color = Green;
VictoryPanel.SetActive(true);
LevelComplete();
StartCoroutine(UpdateScore());
}
else if(timeleft <= 0 && Titles[index].GetComponent<SpriteRenderer>().color == Red)
{
cam.ChangeToEnd();
timertext.color = Red;
GameOverPanel.SetActive(true);
}
}
if(Time.timeSinceLevelLoad >= delay)
{
CountDownAnimimator.SetBool("countdown",false);
cam.ChangeToPlay();
}
}
public void RestartLevel() { timeisUp.gameObject.SetActive(false); Restartbutton.gameObject.SetActive(false); Time.timeScale = 1; timeleft = 40; SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); }
public void NextLevel()
{
//timeisUp.gameObject.SetActive(false);
//Restartbutton.gameObject.SetActive(false);
//Time.timeScale = 1;
timeleft = 40;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void BackToMenu()
{
//timeisUp.gameObject.SetActive(false);
//Restartbutton.gameObject.SetActive(false);
//Time.timeScale = 1;
timeleft = 40;
SceneManager.LoadScene("Menu");
}
IEnumerator SetCollor()
{ for (int i = 0; i < Titles.Length; i++) { while(used.Contains(index)) { index = Random.Range (0,Titles.Length); yield return null; } } used.Add (index); Titles[index].GetComponent().color = Red;
}
void Finish() { //GameObject.Find("Player").SendMessage("Finish"); timertext.color = Green; }
void LevelComplete() { if(PlayerPrefs.GetInt("MaxLevel") == LevelNumber) { PlayerPrefs.SetInt("MaxLevel", PlayerPrefs.GetInt("MaxLevel") + 1); }
}
IEnumerator UpdateScore() { Count = Mathf.MoveTowards(Count, timeleft, Time.deltaTime * 3.0f ); currentScore = currentScore + Count; yield return new WaitForSeconds(0.1f); PlayerPrefs.SetFloat("score",currentScore); PlayerPrefs.Save (); }
}strong text
Answer by NVJOB · Jan 08, 2020 at 04:07 PM
You are using Time.deltaTime in IEnumerator, this is not correct. Get rid of Coroutine UpdateScore, bring the code in Update.
float count, currentScore, timeleft = 1000, countTimeleft;
bool nextLevel;
void Update()
{
//count++; - gets points for something
// nextLevel = true; - level passed
if (nextLevel == false)
{
timeleft -= Time.deltaTime; // time is decreasing
countTimeleft = count + timeleft;
}
else // level passed
{
if (count < countTimeleft) count = Mathf.MoveTowards(count, countTimeleft, Time.deltaTime * 0.1f);
else
{
if (PlayerPrefs.GetFloat("score") != currentScore)
{
PlayerPrefs.SetFloat("score", currentScore);
PlayerPrefs.Save();
}
}
}
}
not work, I did not get, where to put levelcomplete in this code, give me example with my code, I got confused!
In void LevelComplete() add nextLevel = true; void LevelComplete() { if (PlayerPrefs.GetInt("$$anonymous$$axLevel") == LevelNumber) { PlayerPrefs.SetInt("$$anonymous$$axLevel", PlayerPrefs.GetInt("$$anonymous$$axLevel") + 1); nextLevel = true; } }
When starting the next level, again switch nextLevel to nextLevel = false;
In general, the answer lies in line 12. I gave you an example.
like this? public Text scoretext; float currentScore; float Count = 0; float countTimeleft; bool nextLevel;
void Start() { currentScore = PlayerPrefs.GetFloat("score"); nextLevel = false; } void Update() { if(timeleft > 0 && Time.time >= nextTime -1) { if(Timer && nextLevel == false) { timeleft -= Time.deltaTime; countTimeleft = Count + timeleft; go = true; } else { if (Count < countTimeleft) { Count = $$anonymous$$athf.$$anonymous$$oveTowards(Count, countTimeleft, Time.deltaTime * 0.1f); } else { if (PlayerPrefs.GetFloat("score") != currentScore) { PlayerPrefs.SetFloat("score", currentScore); PlayerPrefs.Save(); } } }
if( title1.GetComponent().color == Green ) {
cam.ChangeToEnd();
Timer = false;
timertext.color = Green;
VictoryPanel.SetActive(true);
LevelComplete();
//StartCoroutine(UpdateScore());
}
void LevelComplete() { if(PlayerPrefs.GetInt("$$anonymous$$axLevel") == LevelNumber) { PlayerPrefs.SetInt("$$anonymous$$axLevel", PlayerPrefs.GetInt("$$anonymous$$axLevel") + 1); } nextLevel = true; }
}
thank you man, it word well, i'm a idiot! I hadn't changed count with currentscore!
Answer by logicandchaos · Jan 08, 2020 at 03:59 PM
Well I'm not sure about using MoveTowards or a coroutine to do it.. Also saving it to playerprefs all the time is not the way to do it.. you should only save and load to playerprefs when you are changing levels. I usually use 2 seperate variables currentScore and finalScore.. Anyways I feel like your issue is that you are calling your coroutine in your update and that because you don't set the conditions to call it in the if statement, if(title1.GetComponent().color == Green ) , it is being called each update and you have multiple coroutines running and adding to the score. You should try to define what you want to do as simply as you can and then rewrite your score code. In programming simplicity is king!
hi logicandchaos, yes i know call in update it's wrong but idon't use ontrigger or something similar and i'm use two separate variable (current score and count) without using += it's work well, you can give me an example?