I am tring to make a highscore from coins

I m working on an endless runner 3D project,

Now I can collect coins and coins are written on left corner up

I want to add a script that counts the coins and writes as a highscore when the player is dead to the gameoverMenu, under GAME OVER here:

This is my coin script :

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


public class Coin : MonoBehaviour
{
   
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(100 * Time.deltaTime, 0, 0);


    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            FindObjectOfType<AudioManager>().PlaySound("CoinSound");
            PlayerManager.numberOfCoins += 1;

            //Debug.Log("Coins" + PlayerManager.numberOfCoins);
            Destroy(gameObject);

           
        }

    }
}

AND THIS IS PLAYERMANAGER script

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

public class PlayerManager : MonoBehaviour
{
    public static bool gameOver;
    public GameObject gameoverPanel;

    public static bool isGameStarted;
    public GameObject startingText;

    public static int numberOfCoins;
    public Text coinsText;
    public Text highscoreText;


    void Start()
    {
       
        gameOver = false;
        Time.timeScale = 1;
        isGameStarted = false;
        numberOfCoins = 0;
    }

    private void Awake()
    {
       
    }
    void Update()
    {
       

        if (gameOver)
        {
            Time.timeScale = 0;
            gameoverPanel.SetActive(true);


        }
        coinsText.text = "Coins:" + numberOfCoins;
      

        if (SwipeManager.tap)
        {
            isGameStarted = true;
            Destroy(startingText);
        }

    }
}

I THINK I HAVE TO PUT THE COIN MANAGEMENT THERE I TRIED WITH SOME TUTORS BUT DIDNT HELP

here also PLAYERCONTROLLER script :

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

public class PlayerController : MonoBehaviour
{

    private CharacterController controller;
    private Vector3 direction;
    public float forwardSpeed;
    public float maxSpeed;

    private int desiredLane = 1; //0 left 1 middle 2 right
    public float laneDistance = 4; // distance between two lanes

    public float JumpForce;
    public float Gravity = -20;

    public Animator animator;
    private bool isSliding = false;
    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (!PlayerManager.isGameStarted)
            return;

        if(forwardSpeed < maxSpeed)
            forwardSpeed += 0.1f * Time.deltaTime;

        animator.SetBool("isGameStarted", true);
        direction.z = forwardSpeed;

        animator.SetBool("isGrounded", controller.isGrounded);

        if (controller.isGrounded)
        {
            direction.y = -1;
            if (SwipeManager.swipeUp)
            {
                Jump();
            }
        }
        else
            direction.y += Gravity * Time.deltaTime;

        if (SwipeManager.swipeDown && !isSliding)
        {
            StartCoroutine(Slide());
        }
       
        if (SwipeManager.swipeRight)
        {
            desiredLane++;
            if (desiredLane == 3)
                desiredLane = 2;
        }

        if (SwipeManager.swipeLeft)
        {
            desiredLane--;
            if (desiredLane == -1)
                desiredLane = 0;
        }

        Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;

        if (desiredLane == 0)

        {
            targetPosition += Vector3.left * laneDistance;
        }
        else if (desiredLane == 2)
        {
            targetPosition += Vector3.right * laneDistance;
        }

        // transform.position = Vector3.Lerp(transform.position, targetPosition, 80 * Time.deltaTime);
        // controller.center = controller.center;

        if (transform.position == targetPosition)
            return;
        Vector3 diff = targetPosition - transform.position;
        Vector3 moveDir = diff.normalized * 25 * Time.deltaTime;
        if (moveDir.sqrMagnitude < diff.sqrMagnitude)
            controller.Move(moveDir);
        else
            controller.Move(diff);
    }

    private void FixedUpdate()
    {
        if (!PlayerManager.isGameStarted)
            return;
        controller.Move(direction * Time.fixedDeltaTime);
    }

    private void Jump()
   
    {
        direction.y = JumpForce;
    }

    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (hit.transform.tag == "Obstacle")
        {
            PlayerManager.gameOver = true;
            FindObjectOfType<AudioManager>().PlaySound("GameOverSound");
        }
    }
   
    private IEnumerator Slide()
   
    {
        isSliding = true;
        animator.SetBool("isSliding", true);
        controller.center = new Vector3(0, -0.5f, 0);
        controller.height = 1;
        yield return new WaitForSeconds(1f);

        controller.center = new Vector3(0, 0, 0);
        controller.height = 2;

        animator.SetBool("isSliding", false);
        isSliding = false;
    }
}

Sorry I am confused as to your exact issue? What exactly are you having a problem with?

Are you having a problem counting the coins? Or is the problem displaying the amount of coins collected on the Canvas/GUI? Or is it something else?

1 Like

You’re already doing what you are asking. You are counting your coins, and displaying them.

If you wish to have a highscore you can implement it in a similar fashion, although I would rethink your score being equal to coins as it seems a bit pointless. Maybe high score can be something like distance travelled + coins? If you don’t want this, simply create another display for “Hiscore” and have it display your coins value the same way as you already are doing.

1 Like

Well yes indeed if I had a ( coin + highscore ) system would be nice, where the player can buy new players with coins, but I donnuw if I can handle the code…

Basically now I m trying to make a highscore of coins, haha sounds weird yes :slight_smile:

Line 43 of your player manage script above updates a ui text field with your score value. You also have a ui text field for hi-score in that script so you can simply update that also when coins changes. But like I said I would calculate hi-score seperately to coins and store it as its own variable.

Also, you are updating the text field every frame (in update). I know you are fairly new to coding, and this is okay while you learn, but is considered bad practice. In my experience I tend to just use update for timers, player movement and the such. Things that actually need to be updated every frame. Some better alternatives for you are properties with getter/setter, or events (Unityevent, Action, C# event). I would recommend getting yourself up to speed with all of these things as they are invaluable parts of your programmer toolbox

Anyway I figured out with this tutor

thanks for your remarks.