Add Coin Score to Time Score

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!

And the coin script is the following:

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

public class Coin : MonoBehaviour
{
   
    public float turnSpeed = 90f;

    // Use this for initialization
    void Start ()
    {
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        transform.Rotate(0, 0, turnSpeed * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name != "Personaje")
        {
            return;
        }

        Destroy(gameObject);
    }
}

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Made it earlier and still couldn’t find anything…I’m a noob at Unity and C# as well, and what I’m trying to do is add de coin score to the time score, and so far both scores are being added to themselves only, that’s why they display the score individually, and not as a sum…

And yet the problem persists. Remember, I’m not telling you “something maybe to try.”

I am actually telling you exactly what I would personally do if I had your code in front of me and running.

The above technique works 100% of the time.

Exactly. You have two totalScores, one totalScore in ScoreManager and another totalScore in Personaje. These are two variables that have the same name but they are not connected in any way. You’ll probably want to get rid of one and only update the other one.

How do I update the scoreManager one? I have to call the function from the Personaje script?