0

When my player dies the score continues on and does not reset, it continues the score from the previous session. I would like to reset back to 0 once the player dies, I've added the two scripts

Score script:

using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine;

public class ScoreScript : MonoBehaviour {

    public static int scoreValue = 0;
    Text score;

    // Use this for initialization
    void Start () {

        score = GetComponent<Text>();

    }

    // Update is called once per frame
    void Update () {

        score.text = " " + scoreValue;
    }
}

Player Script:

using UnityEngine;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour {

    public float jumpForce = 10f;

    public Rigidbody2D rb;
    public SpriteRenderer sr;

    public string currentColor;

    public Color colorCyan;
    public Color colorYellow;
    public Color colorMagenta;
    public Color colorPink;

    void Start ()
    {
        SetRandomColor();
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
        {
            rb.velocity = Vector2.up * jumpForce;
        }
    }

    void OnTriggerEnter2D (Collider2D col)
    {
        if (col.tag == "ColorChanger")
        {
            ScoreScript.scoreValue += 1;
            SetRandomColor();
            Destroy(col.gameObject);
            return;
        }

        if (col.tag != currentColor)
        {
            Debug.Log("GAME OVER!");
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }
    }

    void SetRandomColor ()
    {
        int index = Random.Range(0, 4);

        switch (index)
        {
            case 0:
                currentColor = "Cyan";
                sr.color = colorCyan;
                break;
            case 1:
                currentColor = "Yellow";
                sr.color = colorYellow;
                break;
            case 2:
                currentColor = "Magenta";
                sr.color = colorMagenta;
                break;
            case 3:
                currentColor = "Pink";
                sr.color = colorPink;
                break;
        }
    }
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
ROOT
  • 57
  • 1
  • 3
  • 10

2 Answers2

0

You need to be modifying the static variable inside of your ScoreScript whenever the conditions for death are validated in your Player component.

Judging by the source, it looks like the line:

if (col.tag != currentColor)

denotes the end of a game. If this is the case, you'll want to reference ScoreScript.score after the if statement and set the value of this static variable to 0 to reset the score upon death.

Patrick Bell
  • 769
  • 3
  • 15
  • like this ? `if (col.tag != currentColor) { *ScoreScript.scoreValue = 0;* Debug.Log("GAME OVER!"); SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); }` – ROOT Jul 19 '17 at 19:33
0

It looks like scoreValue is a static variable, meaning it's maintained globally rather than per-instantiation of the ScoreScript behavior.

First off, I would change scoreValue to not be static. Then, just like you have for components such as RigidBody2D, I would add a public reference to the ScoreScript in your Player, drag that object to that field in the Unity editor, and then whenever making changes to scoreValue, use your new local reference to the ScoreScript. (So, scoreScript rather than ScoreScript).

Since like all behaviours, the ScoreScript will be reconstructed when the scene resets, this means it will then start at 0. It's not restarting right now because loading a new scene just remakes objects, it doesn't restart the entire scripting environment along with global static variables.

Katana314
  • 8,429
  • 2
  • 28
  • 36