Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hobene · Apr 02, 2020 at 04:51 PM · playercanvasplayerprefshighscoreshighscore

How can I save highscores and display them for later?

This is my script. I'am able to get the final score in the game but as an int, but my highscore canvas won't show up nor will my score text's update. I also want to display the highscores on a different scene that could be accessed in the main menu. Sorry if I made any beginner mistakes.

Here is my code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Scores : MonoBehaviour
 {
     public Text yourCurrentScoreNumberText;
     public Text score1;
     public Text score2;
     public Text score3;
 
     
     public Canvas HighScoreCanvas;
     private TreeBehavior treeBehavior;
     public GameObject ScoreCounter;
    
   // GameObject[] trees = GameObject.FindWithTag("Tree");
    
    
     // Start is called before the first frame update
     void Start()
     {
         HighScoreCanvas.enabled = false;
     }
     public void ShowHighScoreCanvas()
     {
          
 
 
         if (ScoreCounter.GetComponent<ScoreCounter>().mängLäbi == true)
         {
            
             HighScoreCanvas.enabled = true;
             //score componenti peab panema
             yourCurrentScoreNumberText.text = ScoreCounter.GetComponent<ScoreCounter>().finalScore.ToString();
             
             int score1st = PlayerPrefs.GetInt("bestscore");
 
             if (score1st < ScoreCounter.GetComponent<ScoreCounter>().finalScore)
             {
                 //old best was smaller, so we save the new best
                 PlayerPrefs.SetInt("bestscore", ScoreCounter.GetComponent<ScoreCounter>().finalScore);
                 score1st = ScoreCounter.GetComponent<ScoreCounter>().finalScore;
             }
             else
             {
                 score1.text = score1st.ToString();
             }
         }
 
     
     }
     // Start is called before the first frame update
 
 
         
 
 // Update is called once per frame
     void Update()
     {   
         
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Roger_0123 · Apr 02, 2020 at 06:00 PM

Hello, if you want to access your highscores you have two possibilities:

1) Use DontDestroyOnLoad: in your Scores script, just call DontDestroyOnLoad(gameObject) so this object will not be destroyed when changing scene. Note that if you return later on the game scene another Scores object will be instatiated, so you will get two Score objects. To handle this, you may use

void Start(){ //code if(FindObjectsOfType().lenght > 1){ Destroy(gameObject); } } In any case, this scores are lost when you quit the game.

2) If you want to save your scores in a file, I link here a beautiful tutorial by Brackeys

If your highscores don't come up maybe you have to call HighScoreCanvas.setActive(true) which is different from enabled

Hope this helps!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPref set int and get int 1 Answer

Timer highscore trouble 1 Answer

Can someone help me with my highscore script? 2 Answers

My 3 best high scores are always the same. I always see the 1 best score. Can't get them to change accordingly. 3 Answers

UI 4.6 canvas button, how to make it invisible on gameplay and how to make it visible when player is dead? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges