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;
}
}
}