Search Unity

New Canvas Everytime Game resets!?

Discussion in 'Editor & General Support' started by Jana1108, Jul 5, 2015.

  1. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Hi, in my game I have a canvas that contains the score and highscore labels. I want the score to reset to 0 and the highscore label to stay the same when the game restarts (when the player dies and clicks play again) but instead of resetting the score label to 0, it creates a new canvas and starts writing the current score on that while the other canvas is still there so the numbers look like a mushed mess because they're all together. I don't know what's causing it to do this but I either want to destroy the score label each time the game restarts which won't be ideal, or I want to make it not create a new canvas and instead just reset the score label. Here is my code that I've attached to the Player object below.

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

    public class PlayerController : MonoBehaviour
    {
    public Text scoreText;
    public Text bestText;
    public float bestScore;
    public float score;

    //Movement
    public float jump;
    float moveVelocity;

    void Start()
    {
    score = 0;
    SetScoreText();
    scoreText.text = "0";
    }


    void Update ()
    {

    if(score >= bestScore)
    {
    highscore();
    }


    //Jumping
    if(Input.GetMouseButtonDown(0))
    {
    GetComponent<Rigidbody>().velocity = new Vector2(GetComponent<Rigidbody>().velocity.x, jump);
    }

    Vector3 pos = transform.position;
    pos.z = 0;
    pos.x = 0;
    transform.position = pos;

    }

    void OnTriggerEnter(Collider other)
    {
    if(other.gameObject.CompareTag ("Plane"))
    {
    score += 1;
    SetScoreText();
    }
    }

    void SetScoreText()

    {
    scoreText.text = score.ToString();
    }


    void highscore()
    {
    bestScore = score;
    bestText.text = score.ToString();
    }

    }
     
    Last edited: Jul 5, 2015