- Home /
When die - reset score from the last scene
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<Text>();
score = 0;
}
void Update () {
scoring.text = "Score: " + score.ToString();
}
}
And I have in my collectible script.
<...>
score++;
<...>
Answer by GreenSerpent · Nov 26, 2018 at 10:04 PM
When you die, on whatever script you use to kill the player, access the score script and set the score to 0.
It resets back to zero completely. I want to keep the last score from the previous scene even when I die in my next scene. I just want to lose the points that I receiver during my new scene. Here is the code by the way for player. Die (killbox tag) is at the very end:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class bird2 : $$anonymous$$onoBehaviour {
private Animator ani;
//
public bool rip = false;
// $$anonymous$$ovement speed
public float speed = 2;
// Flap force
public float force = 2000;
private Rigidbody2D body;
AudioSource audioWings;
public AudioClip DeathAudio;
// Declaration for Death Counter
public float DeathCounter;
// Use this for initialization
void Start()
{
body = GetComponent<Rigidbody2D>();
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
ani = GetComponent<Animator>();
audioWings = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
// This checks if the bird is dead or not, if it is then the bird cannot be controlled.
if (rip == false)
{
// Flap Control
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
ani.SetTrigger("up");
body.AddForce(Vector2.up * force);
//Play Flap Sound
audioWings.Play();
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "winterobs") // Detects if bird has entered collidor with tag "winterobs"
{
ani.SetTrigger("dead");
rip = true; //Stops Bird Control
body.velocity = Vector2.zero; //Stops Bird $$anonymous$$ovement
GetComponent<AudioSource>().PlayOneShot(DeathAudio);
}
if (other.tag == "killbox") // Detects for tag on collider again
{
DeathCount.DeathValue += 1; ;
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name); //Reloads the active scene
}
}
}
Answer by riskaanisah · Dec 09, 2018 at 12:24 AM
hey @litafbrah may I ask I have a problem similar to yours
I have a game with 10 questions, where every 1 scene is a question. I want to display the score in each scene, can you help me
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad duplicate prevention code is deleting the original player 2 Answers
(2D) Can't make a scene transition between 2 game objects colliding - 2019 1 Answer
How do you transfer UI Text from one scene to another? 1 Answer
Best way to deal with DontDestroyOnLoad when returning back to the same scene? 1 Answer