Search Unity

Trying to make Gamewide wide scorevalue.

Discussion in 'Scripting' started by viluzi, Aug 2, 2017.

  1. viluzi

    viluzi

    Joined:
    Apr 15, 2017
    Posts:
    13
    So im trying to make a score value that i can add to and it has to be saved in playerprefs. I want to load it in a different scene and display it there with a 2D text. How would i go about doing that. I would love an detailed tutorial for that. Example:
    What objects do i put the script in?
    What scripts to liad it what to save it?
    Etc.
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    723
    Here is an official tutorial for using PlayerPrefs for a high score-- not sure if that's exactly what you're going for, but it should be close. Please give it a look and feel free to ask questions about it.
     
    viluzi likes this.
  3. viluzi

    viluzi

    Joined:
    Apr 15, 2017
    Posts:
    13
    I am not going for high score
    But i think i can use it.
     
  4. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    You should really consider doing some tutorials.. I realized you were asking for a specific one, but just go under the Learn section in the Unity site. This is kind of a basic question. There's nothing at all wrong with asking basic questions though, but that tells me you can probably benefit from doing a few end-to-end tutorials. Not just specific topics, but create a few basic games, like they walk you through on the Unity tutorial site: https://unity3d.com/learn/tutorials

    Anyways, this one here from there is probably right up your alley:
    https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial

    Specifically, here's the part about having a score that updates on screen:
    https://unity3d.com/learn/tutorials...ng-points-and-displaying-score?playlist=17147

    It's been quite a while since I've done this tutorial, so I'm not sure if they mention this, but in order for your score to remain set across various levels, you'll need to ensure that the object that manages the score persists (isn't destroyed) as a new scene is loaded..... https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
     
    viluzi likes this.
  5. AntoineDesbiens

    AntoineDesbiens

    Joined:
    Jun 9, 2014
    Posts:
    734
    As others stated, a few tutorials are probably in order, but here's a detailed example of how to achieve what you desire.

    Put this on a gameobject that will be in the first scene your game loads.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ScoreManager : MonoBehaviour
    4. {
    5.     //static variables persist through scene changes.
    6.     public static ScoreManager Instance;
    7.     int score;
    8.  
    9.     void Awake ()
    10.     {
    11.         //Does an instance of "ScoreManager" currently exists?
    12.         if (Instance != null)
    13.         {
    14.             //It does, let's destroy this instance since it's not needed
    15.             Destroy (gameObject);
    16.         }
    17.         else
    18.         {
    19.             //It doesn't. Let's define this instance as the static instance
    20.             Instance = this;
    21.             //Let's make it so this object won't be destroyed during scene changes
    22.             DontDestroyOnLoad (gameObject);
    23.             //We set the initial value of score as the value we had stored in the player prefs
    24.             score = PlayerPrefs.GetInt ("score");
    25.         }
    26.     }
    27.  
    28.     //Call this method to add to the current score.
    29.     public void AddToScore (int scoreToAdd)
    30.     {
    31.         //We add to the local variable of score
    32.         score += scoreToAdd;
    33.         //We update the player prefs score with the new value
    34.         PlayerPrefs.SetInt ("score", score);
    35.         //We save the player prefs
    36.         PlayerPrefs.Save ();
    37.     }
    38.  
    39.     //Call this method to get the current score
    40.     public int GetScore ()
    41.     {
    42.         //This returns the local score variable. Use this to fetch and display the current score.
    43.         return score;
    44.     }
    45. }
     
  6. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    265
    Did you try using : public static int myInt;
     
  7. viluzi

    viluzi

    Joined:
    Apr 15, 2017
    Posts:
    13
    Antoine, im using your code. Thanks. The only problem is. I have no idea how i can add a specific amount to the score... does it not work in editors play mode or am i missing something? If anyone can help me id really appreciate it:)
     
  8. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    789
    From another script you would call:
    Code (CSharp):
    1. // 5 is just an example, you decide how much score to add
    2. ScoreManager.Instance.AddToScore(5);
     
  9. viluzi

    viluzi

    Joined:
    Apr 15, 2017
    Posts:
    13
    ill try that! Thanks!