Hi, when I save my code I'm getting this error: Assets/Scripts/HighScore.cs(14,33): error CS0019: Operator `>=' cannot be applied to operands of type `string' and `string' This is my code using UnityEngine; using UnityEngine.UI; using System.Collections; public class Highscore : MonoBehaviour { public Text scoreText; public Text bestText; // Use this for initialization void Start () { while(scoreText.text >= bestText.text); { bestText.text = scoreText.text; } } // Update is called once per frame void Update () { } } The aim of this script was to update the best score UI Text to whatever the score was at the time if the score was bigger or equal to the current best score.
Store the best score as a float or int. then convert it to a string to display it. You could ask someone like @Tomnnn for a set of extensions to let you do all your regular logic on strings. But this is generally a bad idea.
You summoned me? Sounds like you called the right person for the job! ...... After reading, I don't think my string maths are necessary. They rarely are, but this isn't a design issue with overflowing numbers. @Jana1108 you should follow the advice of the other(s) here and do something like float.parse or float.tryParse to compare those strings.
Thanks for your reply, I'm only 14 years old and am quite inexperienced with coding, are you able to give me an example of the code you're suggesting please?
Code (csharp): using UnityEngine; using UnityEngine.UI; using System.Collections; public class Highscore : MonoBehaviour { public Text scoreText; public Text bestText; public float bestScore; public float score; // Use this for initialization void Start () { if(score >= bestScore) { bestScore = score; bestText.text = score.ToString(); } } }
Thanks, there don't seem to be any errors but it's not working how I would like it to. I added this script to the ' Player ' game object and I setup all the preferences for the texts and floats I defined but it doesn't seem to load the high score...
I merged this script into my player controller script which is attached to the player in the game scene. I made it so that the if statement if(score >= bestScore) { highscore(); } is in void update and then I made a highscore void which contains this: void highscore() { bestScore = score; bestText.text = score.ToString(); } For some reason the highscore label isn't updating when the score is larger than it?
Sure using UnityEngine; using UnityEngine.UI; using System.Collections; public class PlayerController : MonoBehaviour { public Text Score; public Text Best; public Text scoreText; public Text bestText; public float bestScore; public float score; //Movement public float jump; float moveVelocity; public Text countText; public int count; void Start() { count = 0; SetCountText(); Score.text = "0"; } void Update () { if(score >= bestScore) { highscore(); } //Jumping if(Input.GetMouseButtonDown(0)) { GetComponent<Rigidbody>().velocity = new Vector2(GetComponent<Rigidbody>().velocity.x, jump); } Vector3 pos = transform.position; pos.z = 0; pos.x = 0; transform.position = pos; } void OnTriggerEnter(Collider other) { if(other.gameObject.CompareTag ("Plane")) { count = count + 1; SetCountText(); } } void SetCountText() { countText.text = count.ToString(); } void highscore() { bestScore = score; bestText.text = score.ToString(); } }
There is nowhere were you add to score. Are score and count meant to refer to the same thing? If so remove one and replace it throughout.
Yes, count came from a tutorial I was learning off. Is this what you meant? using UnityEngine; using UnityEngine.UI; using System.Collections; public class PlayerController : MonoBehaviour { public Text Score; public Text Best; public Text scoreText; public Text bestText; public float bestScore; public float score; //Movement public float jump; float moveVelocity; public int count; void Start() { count = 0; SetScoreText(); Score.text = "0"; } void Update () { if(score >= bestScore) { highscore(); } //Jumping if(Input.GetMouseButtonDown(0)) { GetComponent<Rigidbody>().velocity = new Vector2(GetComponent<Rigidbody>().velocity.x, jump); } Vector3 pos = transform.position; pos.z = 0; pos.x = 0; transform.position = pos; } void OnTriggerEnter(Collider other) { if(other.gameObject.CompareTag ("Plane")) { count = count + 1; SetScoreText(); } } void SetScoreText() { scoreText.text = count.ToString(); } void highscore() { bestScore = score; bestText.text = score.ToString(); } }
More like this Code (csharp): using UnityEngine; using UnityEngine.UI; using System.Collections; public class PlayerController : MonoBehaviour { public Text scoreText; public Text bestText; public float bestScore; public float score; //Movement public float jump; float moveVelocity; void Start() { score = 0; SetScoreText(); Score.text = "0"; } void Update () { if(score >= bestScore) { highscore(); } //Jumping if(Input.GetMouseButtonDown(0)) { GetComponent<Rigidbody>().velocity = new Vector2(GetComponent<Rigidbody>().velocity.x, jump); } Vector3 pos = transform.position; pos.z = 0; pos.x = 0; transform.position = pos; } void OnTriggerEnter(Collider other) { if(other.gameObject.CompareTag ("Plane")) { score += 1; SetScoreText(); } } void SetScoreText() { scoreText.text = score.ToString(); } void highscore() { bestScore = score; bestText.text = score.ToString(); } }
I got this error when I put that code in: Assets/Scripts/PlayerController.cs(20,17): error CS0103: The name `Score' does not exist in the current context
I just changed it to scoreText.text and it's working now, thanks so much! I am having one slight bug though. When the player dies the gameover scene displays the gameover menu, the player clicks play again and then the game starts again except instead of the score label just resetting to 0, it creates a whole new canvas and overlaps the score on top of the previous one so it's hard to read. The same thing is happening with the highscore. It resets the new canvas text to 0 and counts up again so the highscore is pretty much acting as a second score label at the moment. How can I fix this? Also just a quick question... if the player exits out of the game and then launches it again will their high score be saved? If not how can I do this?
There is a good tutorial in the archive on 'saving and persistent data' . A google search should find it. As to the other issue, how are you handling player death? The solution will depend on how you are restarting the level.
I have a trigger that in the game scene, where the score and highscore is displayed and the game is also played, that when the player collides with an enemy the gameover scene shows but I did a do not destroy script for the score and highscore labels so the gameover menu can show them when the game finishes. On the gameover scene there is a playagain ui text with a button component that the player can click. If the player clicks Play Again then the game scene will relaunch.
Got ya. You've got several options. You can load the score stuff in a separate preloader scene that only runs once. That way the stuff isn't recreated. Or you can use a singleton pattern to enforce only a single instance of each item. Long term and for big complex projects this is a bad idea. But it doesn't hurt to learn how a singleton works. Or you can destroy and recreate your UI each time the scene is loaded.
I think the first option would be best suited for my game, would you mind explaining how I could do this please?
Sure. Simply create a scene that contains all of the persistent stuff you need. Add this scene to the build settings, make it scene 0 so it runs first. Then add a script to load the actual first scene.
So would the script have to be set on a timer? I don't want the player to see that screen. I apologise but I don't seem to understand what you're meaning or what the effect of it will be. I am starting the think that the singleton method would possibly suit my game better...
No need for a timer. Just make a new script that looks like this. Add it to an empty GameObject in the preloader scene. It simply waits a frame then loads the next level. Code (csharp): public class Preloader : MonoBehaviour { IEnumerator Start () { yield return null; Application.LoadLevel(1); } }
How would I go about doing the third suggestion you mentioned ' Or you can destroy and recreate your UI each time the scene is loaded. ' Would I still be able to load highscores and the score if I did this?