Hello there,
I have this simple score script for 2D. When you are in the Scene number1 (where main canvas is attached to) and you collect collectibles - the score increases and whenever you die - it resets to zero again. If I manage to complete the level and get (load) to another scene, the score it still there from the previous scene which is fine. Although, in "that next scene", if I pick additional collectible(score), which adds up to my previous scene score, and die, I still keep my new "fake" score. Basically, I can get infinite score.
What I want to do is, as example, if I had a score of 5 from Scene1 and I load a Scene2 with the same score of 5, so in case I collect additional 5 in Scene2 and die, get respawned at the beginning, I want my score to become the one from the Scene1 which is 5 because right now it becomes 10.
Code is really simple:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public static int score;
Text scoring;
public GameObject mainCanvas;
private void Awake()
{
DontDestroyOnLoad(mainCanvas);
}
void Start()
{
scoring = GetComponent();
score = 0;
}
void Update () {
scoring.text = "Score: " + score.ToString();
}
}
And I have in my collectible script.
<...>
score++; <...>
↧