Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Create GUI Text, Attach time/distance based score script, Save highscore!!! BEGGING!!

Discussion in 'Scripting' started by Jeezker, May 1, 2014.

  1. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    Hey, first of all thanks for clicking on this!

    I know there's a lot of stuff about this but I'm so retarded that I can't make it happen.

    The last piece of my puzzle, a 2d android game, is a scoring system, that is attached to a GUI text, because I want to fiddle with the settings and fonts from there.

    I need a scoring system based on time since the level loaded or travelled distance

    I need the highscore to be saved, stored between scenes and after game is quit.

    I need the highscore to be displayed on the GUI just for a period of time until it disappears

    Thank you very very much, I am so desperate about this I am willing to pay for the script or at least add the person to the credits and praise them before I go to bed for a few weeks.
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    This should help you with saving and getting a high score:
    http://answers.unity3d.com/questions/546072/how-do-i-save-high-scores-in-my-game.html

    For the time since the level loaded try this:
    http://docs.unity3d.com/Documentation/ScriptReference/Time-timeSinceLevelLoad.html
    If you're looking to have the score correlate with the time just make a public variable to store the time in (or store it in player prefs temporarily if you're switching scenes) and when you want to display a score have a script do some math to calculate score out of the elapsed time and display it via gui text. Put a simple timer on the gui text to make it display for a short time and when that time ends make it remove the gui text and switch scenes or whatever you want it to do.

    I can't give you much advice on score correlating to distance traveled since I don't know how the traveling occurs, but you could possibly measure the units from the starting position to the ending position if you store the transform of the player object before it ever starts traveling and compare it to its transform position when it stops traveling. If it's a scroller and the player isn't actually traveling any number of units you'll have to find another way to measure it.
     
    Last edited: May 1, 2014
  3. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    Didn't help... I feel like I'm done with this unity thing
     
  4. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    If you show us the code in question, it's easier for us to help.
     
  5. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    I want this to be displayed using GUI text, but from outside of the script
    Also I want the highscore to be saved between scenes and after quit

    var score : int;

    function Update () {
    score = Time.time;
    }
     
  6. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    You could try something like this:

    Code (csharp):
    1.  
    2. public class ScoreController : MonoBehaviour {
    3.  
    4.     private float score; // Time.time is a float, not an int
    5.  
    6.     // Initalization
    7.     void OnStart () {
    8.  
    9.         // Retrieve the score
    10.         score = PlayerPrefs.GetFloat( "score", 0.0f ); // default score is 0.0f
    11.  
    12.     }
    13.  
    14.     // Display the score
    15.     void OnGUI () {
    16.  
    17.         GUI.Label( new Rect( ... ), score.ToString() ); // Give new Rect() the position and size
    18.  
    19.     }
    20.  
    21.     // OnDestroy (ie. when the player quits)
    22.     void OnDestroy () {
    23.  
    24.         // Save the score
    25.         PlayerPrefs.SetFloat( "score", Time.time );
    26.  
    27.     }
    28.  
    29. }
    30.  
    Also, if you need the "score" variable to update real-time, you need to update it inside the Update() or FixedUpdate() method.
     
    Last edited: May 1, 2014
  7. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    You need to use PlayerPrefs to save your highscore.

    But i'm wondering how you made your game if you can't code what you said? You found the scripts on the Internet?
     
  8. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    Thank you but how do I make it show using external gui text component?
     
  9. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    What do you mean by "external gui text component"? Can you show us that part of the code, as well?
     
  10. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    I just showed you everything I have, I don't know what you don't understand, I want the score displayed using GUI Text
     
  11. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Yes, and that's done inside the OnGUI method. Have you even tried my approach? If you did, what didn't work as expected?
     
  12. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    Well the score doesn't show on the GUI, that's what didn't work as expected and I don't know how to implement it. I want the script to try and access GUI Text from outside the script.
     
  13. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Can you please post the relevant code you're having at this point?
     
  14. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    It's the one you wrote....
     
  15. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    To which object have you attached that script? Did you also create the Update() - or FixedUpdate() - method for updating the "score" variable?
     
  16. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    I HAVEN'T! I am new to all this I don't know how that's why I'm here on the forums
     
  17. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Then I suggest you look into more Unity3D tutorials, because you need to understand the basics before starting with the more advanced stuff.

    In your example, I would assume that you have a Player GameObject of some sort. On that Player object you can add components. If you created a C# called ScoreController (or whatever) based on my code, and added that script (ref. "Add Component") to the player, you would probably get closer to where you want to be.
     
  18. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    A little effort goes a long way. You literally posted code that assigned Time.time to a variable and then said "how come this doesn't display my highscore in a GUIText and save it?" Also - writing things like "DESPERATE" and "BEGGING" in your topic titles isn't going to make people respond to you faster.

    Have you done the tutorials in the Learn section?
    Have you read the manual section on GUI scripting?
    Have you read the API documentation for GUI and PlayerPrefs?
     
  19. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
    I dont know what you want from me I already said I'm retarded.
     
  20. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Also - grow up and use mature/professional language please.
     
  21. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    You definitely need to read more of the documentation. Learn things one step at a time and then put it all together. Go to google and search for "unity gui text tutorial" and whatever else you're not sure how to do, figure out how just that one part is done, and once you understand it move on to another part.

    Here's how to get gui text to display. I've commented it for you so you can understand what's going on and even added some style bits in there so you don't just have tiny black text on the screen. Put this script on any object in your scene and it will display a white zero on the screen. You can then change the score variable to be whatever you want - from either this script or another script.

    Code (csharp):
    1. #pragma strict
    2.  
    3. //Set Up GUI Style
    4. var style : GUIStyle;                                   //The style of the GUI text
    5. private var resizeFont : float = 40;                    //The GUI text font size
    6. //Score Label
    7. public var score : float = 0;                           //The score (Set to 0 by default, this variable is public and can be accessed from another script to change it)
    8. private var scoreRect : Rect = Rect(200, 100, 0, 0);    //The position of the score label on the screen (For now only concern yourself with changing the first two numbers in Rect, 200 and 100
    9.                                                         //- that's position from the left of the screen and from the top of the screen.  Change the numbers to move the text around)
    10.  
    11. function Start () {
    12.  
    13.     //Set up the GUI text
    14.     style.normal.textColor = Color.white;               //Change the GUI text color
    15.     style.fontSize = resizeFont;                        //Change the GUI text font size
    16.    
    17. }
    18.  
    19. function OnGUI () {
    20.  
    21.     GUI.Label (scoreRect, score.ToString(), style);     //Display the score text.  ScoreRect is where it's placed, score is the score (0 currently) turned into a string, style is the font size and color
    22.  
    23. }