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 /
  • Help Room /
avatar image
0
Question by BramNH · Oct 17, 2015 at 07:12 PM · 2d gametextfunctionplayerprefshighscores

Highscore not working

Hello,

I am making a 2d catch game where you have to catch falling objects. I am trying to make a HighScore system, but I am really stuck right now..

This is my score script:

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class Score : MonoBehaviour {

 public static Score HS;
 public Text scoreText;
 public Text HighScore;
 public int krolschValue;
 private int score;
 int highScore;

 void start () {
     HS = this;
     score = 0;
     UpdateScore ();
     highScore = PlayerPrefs.GetInt("HighScore1",0);
 }
 void OnTriggerEnter2D () {
     score += krolschValue;
     UpdateScore ();
 }
 void UpdateScore () {
     scoreText.text = "X " + score;
     HighScore.text = "HighScore: " + highScore;
 }
 public void CheckHighScore()
 {
     if(score > highScore)
     {
         Debug.Log ("Saving Score");
         PlayerPrefs.SetInt("HighScore1",score);
     }
 }

}

And this is my LifeManager script (where I am using the function of when I am out of lifes, in the void Update):

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class LifeManager : MonoBehaviour {

 public int startingLives;
 private int lifeCounter;

 private Text theText;

 public GameObject GameOverScreen;

 public GameManager Spawner;


 // Use this for initialization
 void Start () {
     theText = GetComponent<Text>();

     lifeCounter = startingLives;

     Spawner = FindObjectOfType<GameManager>();
 }
 
 // Update is called once per frame
 void Update () {
     if(lifeCounter <= 0)
     {
         GameOverScreen.SetActive(true);
         Spawner.gameObject.SetActive(false);
         Score.HS.CheckHighScore();
     }

     theText.text = "X " + lifeCounter; 
 }

 public void GiveLife()
 {
     lifeCounter++;
 }

 public void TakeLife()
 {
     lifeCounter--;
 }

}

My score is working perfectly, but when I am out of lifes and the GameOver GUI comes over, where the HighScore text is, it always just shows 0.

Please help

Bram

Comment
Add comment · Show 4
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 BramNH · Oct 17, 2015 at 12:00 PM 0
Share

addition:

When I don't catch anything, so I have 0 points, the Highscores text shows no value.

avatar image Oribow · Oct 17, 2015 at 08:43 PM 0
Share

Should you call UpdateScore () here

  if(lifeCounter <= 0)
      {
          GameOverScreen.SetActive(true);
          Spawner.gameObject.SetActive(false);
          Score.HS.CheckHighScore();
 }

too?

avatar image BramNH Oribow · Oct 18, 2015 at 11:44 AM 0
Share

Nope, still the same... thanks for your response.

avatar image BramNH Oribow · Oct 18, 2015 at 11:48 AM 0
Share

$$anonymous$$y console also says this:

NullReferenceException: Object reference not set to an instance of an object Life$$anonymous$$anager.Update () (at Assets/Scripts/Life$$anonymous$$anager.cs:32)

0 Replies

· Add your reply
  • Sort: 

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

34 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

Related Questions

saving a text into a player pref 0 Answers

Help making a high score with player prefs/displaying it 1 Answer

Help with making interactive text animation for a 2D game 0 Answers

Text Mesh pro won't show certain characters 1 Answer

Tooltip doesn't work 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