• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How many different scripts do I need to write to create a highscore system? 1 Answer

Recognising high score when updated with a graphic 0 Answers

Highscoring and changing scripts (Java/UnityScript) 1 Answer

How do I save highscore (Very Simple) 3 Answers

Is there a decent tutorial for a local high score table (android)? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges