Search Unity

How to use my score

Discussion in 'Scripting' started by matstacy, Feb 13, 2020.

  1. matstacy

    matstacy

    Joined:
    Dec 10, 2019
    Posts:
    15
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.  
    9.     public Text scoreText;
    10.     public int score;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         scoreText.text = "Clicks" + score;
    21.  
    22.         if (Input.GetMouseButtonDown(0))
    23.         {
    24.             score++;
    25.         }
    26.     }
    27. }
    28.  
    how can i reference the score in another scene. if i want to have the score of clicks shown in the next scene how could i do this? thanks for any help
     
    Last edited: Feb 14, 2020
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    1,771
    @matstacy

    Please use code tags... you can edit your message with edit button.

    You can use a script in GameObject that you set to DontDestroyOnLoad - this way you can have this object + its script persist even if scene loads.

    You can use PlayerPrefs to save and load the value.

    You can use any other available C# or Unity serialization method to save and load your data.
     
  3. matstacy

    matstacy

    Joined:
    Dec 10, 2019
    Posts:
    15
    thanks for your help
     
  4. matstacy

    matstacy

    Joined:
    Dec 10, 2019
    Posts:
    15
    So for me to reference or show the score on another scene do i just use the same script in new scene and it will show me the same score? or do i need to make a new object/script that will have the variable of scoretext? im still really confused sorry if im being a pain with easy Questions
     
  5. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    35
    You can use a global static variable to keep track of your score. Some people will say not to do this, and they are right is some circumstances, but it works fine for a simple game.
    I have a SCENE object that runs a SR_Scene script. That script declares global static variables ....

    Code (CSharp):
    1. public static class Global
    2.     {
    3.         public static int GameDifficulty = 3; //0 = easy, 1 normal, 2 difficult, 3 insane
    4.         public static bool FriendlyFire = false;
    5.         public static int CurrentLevel = 0; //level player is on, to track/increase difficulty
    6.         public static bool LevelCleared = true;
    7. }
    Later in the scene script you can access those value like this for example to increase the CurrentLevel .... Global.CurrentLevel++;
    Other scripts will have to access the Scene script to get or set these variables.
    Example for a script on an enemy spaceship OnEnable() ...
    Code (CSharp):
    1. if (SR_Scene.Global.GameDifficulty > 0)
    2.             {
    3.                 //they get faster
    4.                 MaxSpeed += SR_Scene.Global.GameDifficulty;
    5.                 if(MaxSpeed > SR_Scene.Global.MaxPlayerSpeed) { MaxSpeed = SR_Scene.Global.MaxPlayerSpeed; }
    6.                 //and stronger
    7.                 MaxHull += + SR_Scene.Global.GameDifficulty;
    8.                 if (MaxHull > SR_Scene.Global.MaxHull) { MaxHull = SR_Scene.Global.MaxHull; }
    9.                 CurrentHull = MaxHull;
    10.                 //and shoot faster
    11.                 GunCoolRate += SR_Scene.Global.GameDifficulty * 0.01f;
    12.                 if(GunCoolRate > MaxGunCoolRate)
    13.                 {
    14.                     GunCoolRate = MaxGunCoolRate;
    15.                 }
    16.             }
    In every script that calls the static globals you need to define the scene script like this.
    Code (CSharp):
    1. //declare up top of the enemy script
    2. private GameObject SceneController;
    3. private SR_Scene SceneScript;
    4.  
    5. //define in the awake function of enemy script
    6. SceneController = GameObject.FindWithTag("GameController");
    7. SceneScript = SceneController.GetComponent<SR_Scene>();
     
    Last edited: Feb 14, 2020
unityunity