Search Unity

C# Code Error please help

Discussion in 'Scripting' started by Jana1108, Jul 5, 2015.

  1. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  3. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    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.
     
  4. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Highscore : MonoBehaviour {
    6.     public Text scoreText;
    7.     public Text bestText;
    8.     public float bestScore;
    9.     public float score;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.  
    14.         if(score >= bestScore)
    15.         {
    16.             bestScore = score;
    17.             bestText.text = score.ToString();
    18.         }
    19.  
    20.     }
    21. }
     
  6. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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...
     
    Last edited: Jul 5, 2015
  7. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Mind posting the script? It looks like you are on the right track.
     
  9. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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();
    }


    }
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  11. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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();
    }


    }
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    More like this

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public Text scoreText;
    8.     public Text bestText;
    9.     public float bestScore;
    10.     public float score;
    11.  
    12.     //Movement
    13.     public float jump;
    14.     float moveVelocity;
    15.  
    16.     void Start()
    17.     {
    18.         score = 0;
    19.         SetScoreText();
    20.         Score.text = "0";
    21.     }
    22.  
    23.  
    24.     void Update ()
    25.     {
    26.  
    27.         if(score >= bestScore)
    28.         {
    29.             highscore();
    30.         }
    31.  
    32.  
    33.         //Jumping
    34.         if(Input.GetMouseButtonDown(0))
    35.         {
    36.             GetComponent<Rigidbody>().velocity = new Vector2(GetComponent<Rigidbody>().velocity.x, jump);
    37.         }
    38.  
    39.         Vector3 pos = transform.position;
    40.         pos.z = 0;
    41.         pos.x = 0;
    42.         transform.position = pos;
    43.  
    44.     }
    45.  
    46.     void OnTriggerEnter(Collider other)
    47.     {
    48.         if(other.gameObject.CompareTag ("Plane"))
    49.         {
    50.             score += 1;
    51.             SetScoreText();
    52.         }
    53.     }
    54.  
    55.     void SetScoreText()
    56.  
    57.     {
    58.         scoreText.text = score.ToString();
    59.     }
    60.  
    61.  
    62.     void highscore()
    63.     {
    64.         bestScore = score;
    65.         bestText.text = score.ToString();
    66.     }
    67. }
     
  13. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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
     
  14. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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?
     
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  16. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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.
     
  17. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  18. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    I think the first option would be best suited for my game, would you mind explaining how I could do this please?
     
  19. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  20. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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...
     
  21. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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):
    1. public class Preloader : MonoBehaviour {
    2.     IEnumerator Start () {
    3.         yield return null;
    4.         Application.LoadLevel(1);
    5.     }
    6. }
     
  22. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Ok so I've done this, now what?
     
  23. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    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?
     
  24. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Bump...