Unity point marker

0

I am creating my first project in Unity, a prototype of a video game using C #. It's almost ready but I only need the points marker. At the moment I have done this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Puntuacion : MonoBehaviour {
public float TiempoJugando = 0;
public int Tiempo;
public TextMesh marcador;



void Start()
{
    Tiempo = 0;   
}
void Update ()
{
    TiempoJugando = Time.time;
    Tiempo = (int)TiempoJugando;
    Debug.Log(Tiempo);
    marcador.text = Tiempo.ToString();
}
}

The problem is that when the character dies, this is still active and if you press to play again, he continues to count as if he had never finished the game. Does anyone know how to fix it? Thank you very much!

    
asked by Sergio Chinchilla 15.09.2018 в 18:42
source

1 answer

0

For this you need to make a condition. That is, the counter runs while the character is alive.

void Update ()
{
    if(!PlayerState.die && GameManager.inGame) //Enum que define un estado
    {
      TiempoJugando = Time.time;
      Tiempo = (int)TiempoJugando;
      Debug.Log(Tiempo);
      marcador.text = Tiempo.ToString();
    }
}

In this example, state as a condition a game state, in which I tell you that the character is not dead (he is alive), and the state of the game is in InGame (Being played).

With this in mind, you can add another condition, for when the character dies to be able to save the score that is left, and restart when restarting the game.

In summary. One solution would be to say that the TimePlaying variable will only be equal to the Time.time if the character is alive and / or playing.

    
answered by 22.09.2018 в 00:10