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 NotHardGames · Jan 08, 2018 at 04:41 PM · savesave datasceneshighscores

Displaying highscore in the main menu scene

I need to display the highscore in the main menu and the score is gained in another scene. The script where the points are gained is :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class ballandscore : MonoBehaviour {
     public int timeLeft = 30;
     public Text countdownText;
     public int Score;
     public Text ScoreText;
     public int scoreresult;
     public Text ScoreResult;
     public bool scoresult;
     public GameObject Player1;
 
 
 
     void Start () {
         
         StartCoroutine ("LoseTime");
         Score = 0;
         SetScoreText ();
         scoreresult = 0;
         scoresult = false;
         ScoreResult.text = "";
     }
     
 
     void Update () {
         countdownText.text = ("Time left : " + timeLeft);
         if(timeLeft <= 0){
             StopCoroutine("LoseTime");
             GameOver ();
         }
     }
     IEnumerator LoseTime()
     {
         while (true) {
             yield return new WaitForSeconds (1f);
             timeLeft = timeLeft - 1;
 
         }
 
     }
     void OnCollisionEnter2D(Collision2D colliii){
         if(colliii.gameObject.tag == "ball"){
             Destroy(colliii.gameObject);
             Score = Score + 3;
             scoreresult = scoreresult + 3;
             SetScoreText ();
             timeLeft = timeLeft + 2;
             PlayerPrefs.SetInt ("High Score", scoreresult);
 
         }        
 
 }
 
     void SetScoreText ()
     {
         ScoreText.text =  Score.ToString ();
     }
 
     public void GameOver ()
     {
         ScoreResult.text = "Your Score Is " + scoreresult.ToString();
         Destroy (Player1);
         countdownText.text = "";
         ScoreText.text = "";
         scoresult = true;
 
     }
 
 
 
 }


The script attached to an empty gameobject located in the main menu scene is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class HighscoreScript : MonoBehaviour {
 
 
     public Text HighscoreText;
     public int HigherScore = 0;
     ballandscore theScore;
     public int truescore = 0;
 
     void Start () 
     {
         theScore = GetComponent<ballandscore> ();
         truescore = theScore.scoreresult;
         if (truescore > HigherScore) {
             HigherScore = truescore;
         }
         haighScore ();
     }
 
     void Update(){
     }
 
     void haighScore()
     {
         HighscoreText.text = "Highscore : " + PlayerPrefs.GetInt ("High Score",truescore);
     }
 }

I did a lot of research but most of the things I found were outdated. Thank you for helping.

Comment
Add comment · Show 6
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
avatar image osybear · Jan 08, 2018 at 04:44 PM 0
Share

So what is the problem? seems to me you are doing fine.

avatar image NotHardGames osybear · Jan 08, 2018 at 04:44 PM 0
Share

The Highscore does not show up in the main menu.It stays as "New Text"

avatar image OneCept-Games NotHardGames · Jan 08, 2018 at 04:50 PM 0
Share

and you remember to drag your Score Text object to the Inspector public var?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by OneCept-Games · Jan 08, 2018 at 05:15 PM

theScore is null on return of GetComponent(). Do you have a ballscore script on your GameObject from where you call GetComponent()?

Comment
Add comment · Show 5 · 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
avatar image NotHardGames · Jan 08, 2018 at 05:30 PM 0
Share

Well , the ballandscore script is in the other scene , attached to the player.In the main menu scene , there is only the empty object which has the HighscoreScript attached to it. So I need to attach the ballandscore script on this empty object too ?

avatar image NotHardGames · Jan 08, 2018 at 05:35 PM 0
Share

I think I need to call the scoreresult variable from the ballandscore script to the highscorescript , but I don't know how to do that since they are in different scenes and scripts , and attached to different GameObjects.

avatar image NotHardGames · Jan 08, 2018 at 06:10 PM 0
Share

Ok thanks to your idea I fixed it.I made the gameobject controlled by the player a prefab and put it in the main menu as a decoration so that the ballandscore script was in the main menu scene,to be able to get the component in the Highscorescript.

avatar image OneCept-Games NotHardGames · Jan 08, 2018 at 08:43 PM 0
Share

Glad you fixed it. I guess you do not need all methods in the ballonscore in your main menu, so you should either try to make a script that only manages Set- and Get to/from PlayerPrefs, "High Score" key. But you can of course always attach the ballonscore to an Empty GameObject in your $$anonymous$$ain $$anonymous$$enu scene and leave the variables empty, and then in your code check if they are Null and then do not use them.
But most important that you have fixed it and can continue your development. Good luck with your project!

avatar image NotHardGames OneCept-Games · Jan 09, 2018 at 06:44 AM 0
Share

Thank you !

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

78 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

Related Questions

How to set and save individual highscores for each scene using serialization? 3 Answers

Saving current scene in another scene during gameplay? 1 Answer

options menu UI doesnt save when switching scenes 1 Answer

my text size changes didnt saved by slider 1 Answer

Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 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