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 mcnifftaylor · Apr 22, 2019 at 09:49 PM · highscoreshighscore

why isn't my high score script working?

Here is my Game Controller script. I believe adding a High Score is relatively simple, but for some reason, my code is not working. It won't even update like a normal score. I've tried different extensions on the SetInt and GetInt PlayerPrefs, (,0 ,score ,highScore) but it still did not work. As well as moving my if statement to void update. For more context, this is an extension of Unity's Space Shooter Tutorial.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class GameController : MonoBehaviour
 {
     public GameObject[] hazards;
     public GameObject playerExplosion;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
 
     public Text ScoreText;
     public Text RestartText;
     public Text GameOverText;
     public Text whoText;
     public Text highScoreText;
     public Text winText;
 
     private bool gameOver;
     private bool restart;
     private bool win;
     private int score;
     private int highScore;
 
     void Start()
     {
         gameOver = false;
         restart = false;
         win = false;
         GameOverText.text = "";
         whoText.text = "";
         winText.text = "";
         highScoreText.text = "High Score: " + highScore;
         score = 0;
         highScore = PlayerPrefs.GetInt("High Score", score);
         UpdateScore();
         StartCoroutine(SpawnWaves());
         
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.H))
         {
             SceneManager.LoadScene("HardMode");
         }
         if (Input.GetKeyDown(KeyCode.N))
         {
             SceneManager.LoadScene("NormalMode");
         }
         if (restart)
         {
             if (Input.GetKey("return"))
             {
                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
             }
         }
         {
             if (Input.GetKey("escape"))
                 Application.Quit();
         }
     }
 
     IEnumerator SpawnWaves()
     {
         yield return new WaitForSeconds(startWait);
         while (true)
         {
             for (int i = 0; i < hazardCount; i++)
             {
                 GameObject hazard = hazards[UnityEngine.Random.Range(0, hazards.Length)];
                 Vector3 spawnPosition = new Vector3(UnityEngine.Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate(hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds(spawnWait);
             }
             yield return new WaitForSeconds(waveWait);
 
             if (gameOver)
             {
                 GameOverText.text = "Game Over!";
                 RestartText.text = "Press 'Enter' to Restart";
                 whoText.text = "Created By Taylor McNiff";
                 restart = true;
                 win = false;
                 break;
             }
         }
     }
 
     public void AddScore(int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore();
     }
 
     void UpdateScore()
     {
         ScoreText.text = "Points: " + score;
         if (score >= highScore)
         {
             PlayerPrefs.SetInt("High Score", score);
         }
     }
 
     public void GameOver()
     {
         gameOver = true;
     }
 }
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
0

Answer by highpockets · Apr 22, 2019 at 09:48 PM

In the Start() method the following should be moved to the last line and you should change the highScore int to a string:


 highScoreText.text = "High Score: " + highScore.ToString();


This is because you haven’t set the highScore to any value where you have it, so it will have a value of 0 and the text property takes a string, not an int.


Other than that, I don’t see where you call AddScore(), but I don’t see anything else that stands out. Let me know if the above works.

Cheers

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

169 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 avatar image avatar image avatar image

Related Questions

how do i store highscore locally C# (SIMPLE) 3 Answers

Hey i am trying to add a time score system and time high score system need help. 0 Answers

A way to move "Highscore" Text to Game Over Screen? 1 Answer

High Score Save iOS 0 Answers

GUI Highscore? 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