Hello, guys. Everytime my player collides with a coin it adds 50 points, and every second it adds 100 points. But ingame I can see the time score being added, and when I catch a coin I see the 50 there, but the 50 is not added to the time score, it’s like the same text has 2 scores. Here are my scripts:
My scoreManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public Text puntuacionGameplayText, puntuacionFinalText;
private int scorePerTick = 100;
private int scoreCoin = 50;
private float nextTimeToScore;
private int totalScore = 0;
private bool juegoAcabado = false;
// Use this for initialization
void Start () {
//Empezamos teniendo el cuenta en tiempo para que el score no empiece ya en 100
nextTimeToScore = Time.timeSinceLevelLoad + 1;
}
// Update is called once per frame
void Update () {
if (!juegoAcabado)
{
if (Time.timeSinceLevelLoad >= nextTimeToScore)
{
AumentarScore();
nextTimeToScore = Time.timeSinceLevelLoad + 1;
}
}
}
public void AcabarJuego()
{
juegoAcabado = true;
int maximaPuntuacion = PlayerPrefs.GetInt("Puntuacion", 0);
int puntuacionTotal = GetTotalScore();
string puntuacionString = "Tu puntuación ha sido de \n " + puntuacionTotal;
if (maximaPuntuacion < puntuacionTotal)
{
puntuacionString += "\n ¡Nueva puntuación maxima alcanzada!";
PlayerPrefs.SetInt("Puntuacion", puntuacionTotal);
PlayerPrefs.Save();
}
puntuacionFinalText.text = puntuacionString;
}
void AumentarScore()
{
totalScore += scorePerTick;
puntuacionGameplayText.text = totalScore.ToString();
}
public int GetTotalScore()
{
return totalScore;
}
}
My coin score is within the character script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Personaje : MonoBehaviour {
public float aceleracion = 0.0015f;
public float speed = 5;
public float leftRightSpeed = 5;
public int segundosInmune = 3;
public int vidaMaxima;
public GameObject hitFX;
public AudioSource hitSound;
public Text puntuacionGameplayText, puntuacionFinalText;
private int scoreCoin = 50;
private int totalScore;
private int vidaActual;
private Rigidbody rigidBody;
private Animator animator;
private bool isInmune = false;
private float nextTimeKickInmune = 0;
private float speedMultiply = 0;
public Text lives;
public float jumpForce = 10;
public bool isGround = true;
// Use this for initialization
void Start()
{
rigidBody = GetComponent<Rigidbody>();
animator = GetComponentInChildren<Animator>();
rigidBody.freezeRotation = true;
vidaActual = vidaMaxima;
lives.text = "Lives = " + vidaActual;
}
// Update is called once per frame
void Update()
{
//Vamos a ir incrementando la velocidad a la que tiene que ir en intervalos regulares hasta alcanzar la velocidad que le corresponde
speedMultiply += aceleracion;
if (speedMultiply > 1) speedMultiply = 1;
float realSpeed = speedMultiply * speed;
float realLeftRightSpeed = speedMultiply * leftRightSpeed;
Vector3 movimiento = Vector3.forward * realSpeed * Time.deltaTime;
if (Input.GetKey("left"))
{
movimiento += Vector3.left * realLeftRightSpeed * Time.deltaTime;
}
if (Input.GetKey("right"))
{
movimiento += Vector3.right * realLeftRightSpeed * Time.deltaTime;
}
if (Input.GetKeyDown("space") && isGround)
{
GetComponent<Rigidbody>().velocity = new Vector3(rigidBody.velocity.x, jumpForce);
animator.SetTrigger("Jump");
isGround = false;
}
transform.position += movimiento;
if (isInmune)
{
if (Time.timeSinceLevelLoad >= nextTimeKickInmune)
{
AcabarInmunidad();
}
}
lives.text = "Lives = " + vidaActual;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Floor")
{
isGround = true;
}
}
public void DetenerPersonaje()
{
speed = 0;
leftRightSpeed = 0;
animator.SetTrigger("Detener");
}
void CoinScore()
{
totalScore += scoreCoin;
puntuacionGameplayText.text = totalScore.ToString();
}
public void RestarVida()
{
if (!isInmune)
{
hitFX.SetActive(true);
hitSound.pitch = Random.Range(0.8f, 1.2f);
hitSound.Play();
vidaActual--;
Debug.Log("Vida actual " + vidaActual);
}
}
public bool EstaMuerto()
{
if (vidaActual <= 0) return true;
else return false;
}
public void AcabarInmunidad()
{
//rigidBody.isKinematic = false;
isInmune = false;
}
public void EmpezarInmunidad()
{
if (!isInmune)
{
//rigidBody.isKinematic = true;
isInmune = true;
nextTimeKickInmune = Time.timeSinceLevelLoad + segundosInmune;
}
}
public void BloqueGolpeado()
{
RestarVida();
EmpezarInmunidad();
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "Enemy")
{
BloqueGolpeado();
}
if (other.gameObject.tag == "Coin")
{
CoinScore();
}
}
}
And here is a video of that situation:
thanks in advance for any help!