Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 vincentamato · Dec 13, 2014 at 08:33 PM · playerprefsscoretextmeshhighscores

Taking high score from anther script and then displaying it

Hi. In my game the player is climbing up a wall while objects are trying to knock him off. His score is how many seconds he has survived for. I am trying to make a high score but its not going so well. Here is what I have now:

 using UnityEngine;
 using System.Collections;
 
 public class AddToScore : MonoBehaviour {
 
     private int now = 3; 
     public int score = 0; 
     private string second = " second";
     private string seconds = " seconds"; 
     public int highScore = 0; 
     public GUIText scoreLabel; 
 
 
     void Start(){
         scoreLabel.text = score.ToString() + seconds;
         InvokeRepeating ("AdToScore", 1, 1);
     }
 
     void Update(){
         if (score > 0) {
             second = " seconds"; 
         }
 
         if (score > highScore) {
             PlayerPrefs.SetInt("highscore", score);
             PlayerPrefs.Save();
             highScore = score; 
         }
     }
 
     void AdToScore(){
         if (now > 0) {
             score = score + 1; 
             scoreLabel.text = score.ToString() + second;
 
         }
     }
 }
 

I am sure there is something wrong with creating and saving this high score because it is not displaying in a text mesh. Here is the code for that:

 using UnityEngine;
 using System.Collections;
 
 public class DisplayHighScore : MonoBehaviour {
 
     public TextMesh highScore78;
     private AddToScore addtoscoreReference; 
 
     // Use this for initialization
     void Start () {
     
         PlayerPrefs.GetInt ("highScore");
 
         highScore78.text = addtoscoreReference.highScore + " seconds"; 
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

Please ignore the fact that there is a 78 in the variable name for the text mesh. I just use it to differentiate from other variables. Anyways please help me. Thanks!

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

2 Replies

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

Answer by ahmedbenlakhdhar · Dec 13, 2014 at 09:08 PM

Try to write (highScore78.text = addtoscoreReference.highScore + " seconds";) in Update instead of Start.

If you want to execute the instruction only once then try this:

 using UnityEngine;
 using System.Collections;
  
 public class DisplayHighScore : MonoBehaviour {
  
     public TextMesh highScore78;
     private AddToScore addtoscoreReference; 
 
     private bool executed = false;
  
     // Use this for initialization
     void Start () {

     }
      
     // Update is called once per frame
     void Update () {
         if(!executed){
             highScore78.text = addtoscoreReference.highScore + " seconds";
             executed = true;
         }
     }
 }
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
avatar image
0

Answer by Grim_Darknight · Dec 13, 2014 at 09:05 PM

You are missing an OnGUI() function. you can use legacy or new UI. http://docs.unity3d.com/Manual/UISystem.html

Comment
Add comment · Show 2 · 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 vincentamato · Dec 13, 2014 at 09:25 PM 0
Share

@Grim_Darknight In sorry in the documentation link I couldn't find anything. Also I am not using UI. Every button and label are visible game objects.

avatar image vincentamato · Dec 13, 2014 at 09:25 PM 0
Share

@Grim_Darknight Im sorry in the documentation link I couldn't find anything. Also I am not using UI. Every button and label are visible game objects.

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

27 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

Related Questions

in this code whenever score is gretater than highscore then old highscore becomes zero and after the game ends it get updated 0 Answers

Saving final score and displaying on main menu 1 Answer

HighScore Manage Using PlayerPrefs 1 Answer

PlayerPrefs not saving my variable value for HighScore System 3 Answers

PlayerPref set int and get int 1 Answer


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