Search Unity

I am tring to make a highscore from coins

Discussion in 'Scripting' started by Evrensel, Jul 17, 2020.

  1. Evrensel

    Evrensel

    Joined:
    Jan 19, 2020
    Posts:
    6
    I m working on an endless runner 3D project,

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

    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:
    22.jpg


    This is my coin script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Coin : MonoBehaviour
    7. {
    8.    
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         transform.Rotate(100 * Time.deltaTime, 0, 0);
    18.  
    19.  
    20.     }
    21.     private void OnTriggerEnter(Collider other)
    22.     {
    23.         if (other.tag == "Player")
    24.         {
    25.             FindObjectOfType<AudioManager>().PlaySound("CoinSound");
    26.             PlayerManager.numberOfCoins += 1;
    27.  
    28.             //Debug.Log("Coins" + PlayerManager.numberOfCoins);
    29.             Destroy(gameObject);
    30.  
    31.            
    32.         }
    33.  
    34.     }
    35. }
    36.  

    AND THIS IS PLAYERMANAGER script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerManager : MonoBehaviour
    7. {
    8.     public static bool gameOver;
    9.     public GameObject gameoverPanel;
    10.  
    11.     public static bool isGameStarted;
    12.     public GameObject startingText;
    13.  
    14.     public static int numberOfCoins;
    15.     public Text coinsText;
    16.     public Text highscoreText;
    17.  
    18.  
    19.     void Start()
    20.     {
    21.        
    22.         gameOver = false;
    23.         Time.timeScale = 1;
    24.         isGameStarted = false;
    25.         numberOfCoins = 0;
    26.     }
    27.  
    28.     private void Awake()
    29.     {
    30.        
    31.     }
    32.     void Update()
    33.     {
    34.        
    35.  
    36.         if (gameOver)
    37.         {
    38.             Time.timeScale = 0;
    39.             gameoverPanel.SetActive(true);
    40.  
    41.  
    42.         }
    43.         coinsText.text = "Coins:" + numberOfCoins;
    44.      
    45.  
    46.         if (SwipeManager.tap)
    47.         {
    48.             isGameStarted = true;
    49.             Destroy(startingText);
    50.         }
    51.  
    52.     }
    53. }
    54.  

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

    here also PLAYERCONTROLLER script :


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     private CharacterController controller;
    9.     private Vector3 direction;
    10.     public float forwardSpeed;
    11.     public float maxSpeed;
    12.  
    13.     private int desiredLane = 1; //0 left 1 middle 2 right
    14.     public float laneDistance = 4; // distance between two lanes
    15.  
    16.     public float JumpForce;
    17.     public float Gravity = -20;
    18.  
    19.     public Animator animator;
    20.     private bool isSliding = false;
    21.     void Start()
    22.     {
    23.         controller = GetComponent<CharacterController>();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         if (!PlayerManager.isGameStarted)
    29.             return;
    30.  
    31.         if(forwardSpeed < maxSpeed)
    32.             forwardSpeed += 0.1f * Time.deltaTime;
    33.  
    34.         animator.SetBool("isGameStarted", true);
    35.         direction.z = forwardSpeed;
    36.  
    37.         animator.SetBool("isGrounded", controller.isGrounded);
    38.  
    39.         if (controller.isGrounded)
    40.         {
    41.             direction.y = -1;
    42.             if (SwipeManager.swipeUp)
    43.             {
    44.                 Jump();
    45.             }
    46.         }
    47.         else
    48.             direction.y += Gravity * Time.deltaTime;
    49.  
    50.         if (SwipeManager.swipeDown && !isSliding)
    51.         {
    52.             StartCoroutine(Slide());
    53.         }
    54.        
    55.         if (SwipeManager.swipeRight)
    56.         {
    57.             desiredLane++;
    58.             if (desiredLane == 3)
    59.                 desiredLane = 2;
    60.         }
    61.  
    62.         if (SwipeManager.swipeLeft)
    63.         {
    64.             desiredLane--;
    65.             if (desiredLane == -1)
    66.                 desiredLane = 0;
    67.         }
    68.  
    69.         Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
    70.  
    71.         if (desiredLane == 0)
    72.  
    73.         {
    74.             targetPosition += Vector3.left * laneDistance;
    75.         }
    76.         else if (desiredLane == 2)
    77.         {
    78.             targetPosition += Vector3.right * laneDistance;
    79.         }
    80.  
    81.         // transform.position = Vector3.Lerp(transform.position, targetPosition, 80 * Time.deltaTime);
    82.         // controller.center = controller.center;
    83.  
    84.         if (transform.position == targetPosition)
    85.             return;
    86.         Vector3 diff = targetPosition - transform.position;
    87.         Vector3 moveDir = diff.normalized * 25 * Time.deltaTime;
    88.         if (moveDir.sqrMagnitude < diff.sqrMagnitude)
    89.             controller.Move(moveDir);
    90.         else
    91.             controller.Move(diff);
    92.     }
    93.  
    94.     private void FixedUpdate()
    95.     {
    96.         if (!PlayerManager.isGameStarted)
    97.             return;
    98.         controller.Move(direction * Time.fixedDeltaTime);
    99.     }
    100.  
    101.     private void Jump()
    102.    
    103.     {
    104.         direction.y = JumpForce;
    105.     }
    106.  
    107.     private void OnControllerColliderHit(ControllerColliderHit hit)
    108.     {
    109.         if (hit.transform.tag == "Obstacle")
    110.         {
    111.             PlayerManager.gameOver = true;
    112.             FindObjectOfType<AudioManager>().PlaySound("GameOverSound");
    113.         }
    114.     }
    115.    
    116.     private IEnumerator Slide()
    117.    
    118.     {
    119.         isSliding = true;
    120.         animator.SetBool("isSliding", true);
    121.         controller.center = new Vector3(0, -0.5f, 0);
    122.         controller.height = 1;
    123.         yield return new WaitForSeconds(1f);
    124.  
    125.         controller.center = new Vector3(0, 0, 0);
    126.         controller.height = 2;
    127.  
    128.         animator.SetBool("isSliding", false);
    129.         isSliding = false;
    130.     }
    131. }
    132.  
     
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    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?
     
    Evrensel likes this.
  3. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    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.
     
    Evrensel likes this.
  4. Evrensel

    Evrensel

    Joined:
    Jan 19, 2020
    Posts:
    6
    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 :)
     
  5. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    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
     
  6. Evrensel

    Evrensel

    Joined:
    Jan 19, 2020
    Posts:
    6
    Anyway I figured out with this tutor
    thanks for your remarks.