Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to fix "+" instead of space?

Discussion in 'Scripting' started by groch, May 31, 2018.

  1. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Hello everyone. In my game, after typing your username for leaderboard, there is a "+" instead of a space. How can I fix this? I am more a graphic designer than programmist, sorry if this is a very simple question.

    You can see it here (first and third place):

     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  3. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Sure, here it is:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class DisplayHighscores : MonoBehaviour {
    6.  
    7.     public Text[] highscoreText;
    8.     HighScores highscoreManager;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         for (int i =0; i < highscoreText.Length; i ++)
    14.         {
    15.             highscoreText[i].text = i+1 + ". Fetching...";
    16.         }
    17.  
    18.         highscoreManager = GetComponent<HighScores> ();
    19.  
    20.         StartCoroutine ("RefreshHighscores");
    21.     }
    22.  
    23.     public void OnHighscoresDownloaded (Highscore[] highscoreList)
    24.     {
    25.         for (int i =0; i < highscoreText.Length; i ++)
    26.         {
    27.             highscoreText[i].text = i+1 + ". ";
    28.             if (highscoreList.Length > i)
    29.             {
    30.                 highscoreText[i].text += highscoreList[i].username + " - " + highscoreList[i].score;
    31.             }
    32.         }
    33.     }
    34.  
    35.     IEnumerator RefreshHighscores()
    36.     {
    37.         while (true)
    38.         {
    39.             highscoreManager.DownloadHighscores();
    40.             yield return new WaitForSeconds(30);
    41.         }
    42.     }
    43.  
    44. }
    45.  
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That doesn't look to be an issue. I suppose one other place to check is where you get the username?
     
  5. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Hmm, maybe that one?

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6. public class LeaderBoardSampleOriginal : MonoBehaviour {
    7.    
    8.     public int totalScore = 0;
    9.     string playerName = "";
    10.     string code = "";
    11.  
    12.     // Przyciski:
    13.  
    14.     public GameObject boxLauncher1;
    15.  
    16.     public GameObject boxLauncher2;
    17.  
    18.     public GameObject saveScoreButton;
    19.  
    20.     public GameObject seeHighScores;
    21.  
    22.     public GameObject scoreSaved;
    23.  
    24.     public GameObject tutorialText;
    25.  
    26.     public GameObject tutorialArrow;
    27.  
    28.     public GameObject tutorialCup;
    29.  
    30.     public GameObject tutorialStart;
    31.  
    32.     public string highScores;
    33.  
    34.     // Inne:
    35.  
    36.     public string restartGame;
    37.  
    38.     public bool isPaused;
    39.  
    40.     public GameObject canvas;
    41.  
    42.     public bool hasLost = false;
    43.  
    44.     public GUISkin scoreboardSkin;
    45.  
    46.     Text userNameInputText;
    47.  
    48.  
    49.     enum gameState {
    50.         waiting,
    51.         running,
    52.         enterscore,
    53.         leaderboard
    54.     };
    55.    
    56.     gameState gs;
    57.    
    58.    
    59.     // Reference to the dreamloLeaderboard prefab in the scene
    60.    
    61.     dreamloLeaderBoard dl;
    62.  
    63.  
    64.     public void NewGame()
    65.     {
    66. //        bannerView.Hide();
    67. //        bannerView.Destroy();
    68. //        interstitial.Destroy();
    69.         Application.LoadLevel(restartGame);
    70.  
    71.     }
    72.  
    73.  
    74.     public void HighScores()
    75.     {
    76. //        bannerView.Hide();
    77. //        bannerView.Destroy();
    78. //        interstitial.Destroy();
    79.         Application.LoadLevel(highScores);
    80.  
    81.  
    82.     }
    83.  
    84.     public void TutorialStart()
    85.     {
    86.         tutorialCup.SetActive(false);
    87.         tutorialArrow.SetActive(false);
    88.         tutorialText.SetActive(false);
    89.         tutorialStart.SetActive(false);
    90.         boxLauncher1.SetActive(true);
    91.         boxLauncher2.SetActive(true);
    92.     }
    93.  
    94.     public void SaveScore()
    95.     {
    96.         scoreSaved.SetActive(true);
    97.         seeHighScores.SetActive(true);
    98.         saveScoreButton.SetActive(false);
    99.         if (dl.publicCode == "") Debug.LogError("You forgot to set the publicCode variable");
    100.         if (dl.privateCode == "") Debug.LogError("You forgot to set the privateCode variable");
    101.  
    102.  
    103.         dl.AddScore(this.playerName, totalScore);
    104.  
    105.  
    106.         this.gs = gameState.enterscore;
    107.  
    108.  
    109.  
    110.     }
    111.  
    112.  
    113.     void Awake()
    114.     {
    115.         boxLauncher1.SetActive(false);
    116.         boxLauncher2.SetActive(false);
    117.     }
    118.  
    119.     void Start ()
    120.     {
    121.  
    122.  
    123.  
    124.         scoreSaved.SetActive(false);
    125.         seeHighScores.SetActive(false);
    126.  
    127.         // get the reference here...
    128.         this.dl = dreamloLeaderBoard.GetSceneDreamloLeaderboard();
    129.  
    130.         // get the other reference here
    131.         //this.pc = dreamloPromoCode.GetSceneDreamloPromoCode();
    132.  
    133.         this.gs = gameState.waiting;
    134.     }
    135.    
    136.     void Update ()
    137.     {
    138.  
    139.  
    140.  
    141.         if(hasLost)
    142.         {
    143.             this.gs = gameState.enterscore;
    144.         }
    145.  
    146.         if (isPaused)
    147.         {
    148.             Time.timeScale = 0f;
    149.             this.gs = gameState.running;
    150.         }
    151.  
    152.         else
    153.         {
    154.             Time.timeScale = 1f;
    155.         }
    156.         if (Input.GetKeyDown(KeyCode.Escape))
    157.         {
    158.             isPaused = !isPaused;
    159.         }
    160.  
    161.     }
    162.  
    163.     void OnTriggerEnter2D()
    164.     {
    165.         hasLost = true;
    166.     }
    167.  
    168.     void OnGUI ()
    169.     {
    170.        
    171.         GUI.skin = scoreboardSkin;
    172.        
    173.  
    174.        
    175.         GUILayout.BeginVertical ();
    176.         if (this.gs == gameState.waiting || this.gs == gameState.running) {
    177.  
    178.         }
    179.  
    180.  
    181.         if (hasLost) {
    182.  
    183.             if (this.gs == gameState.enterscore) {
    184.                 GUILayout.BeginArea (new Rect (Screen.width / 2 - 125, Screen.height / 2 - 100, Screen.width / 1, Screen.height / 2));
    185.                 GUILayout.Label ("Your Score: " + this.totalScore.ToString ());
    186.                 GUILayout.BeginHorizontal ();
    187.                 GUILayout.Label ("Your Name: ");
    188.                 GUILayout.EndHorizontal ();
    189.                 //GUILayout.BeginArea(new Rect(Screen.width * .10f, Screen.height * .13f, Screen.width * .57f, Screen.height * .55f));
    190.                 GUILayout.BeginHorizontal ();
    191.                 this.playerName = GUILayout.TextField (this.playerName);
    192.                 //GUILayout.EndArea();
    193.                 GUILayout.EndHorizontal ();
    194.                 GUILayout.EndArea ();
    195.             }
    196.         }
    197.        
    198.         if (this.gs == gameState.leaderboard) {
    199.             GUILayout.Label ("High Scores:");
    200.             List<dreamloLeaderBoard.Score> scoreList = dl.ToListHighToLow ();
    201.            
    202.             if (scoreList == null) {
    203.                 GUILayout.Label ("(loading...)");
    204.             } else {
    205.                 int maxToDisplay = 20;
    206.                 int count = 0;
    207.                 foreach (dreamloLeaderBoard.Score currentScore in scoreList) {
    208.                     count++;
    209.                     GUILayout.BeginHorizontal ();
    210.                     GUILayout.Label (currentScore.playerName);
    211.                     GUILayout.Label (currentScore.score.ToString ());
    212.                     GUILayout.EndHorizontal ();
    213.  
    214.                     if (count >= maxToDisplay)
    215.                         break;
    216.                 }
    217.             }
    218.         }
    219.  
    220.  
    221.         GUILayout.BeginHorizontal ();
    222.         GUILayout.EndHorizontal ();
    223.         GUILayout.Space (50);
    224.         GUILayout.EndVertical ();
    225.     }
    226. }
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Again, doesn't seem to be the issue. :) Maybe the code to add the score? lol

    I mean, if you debug log the name, I'm sure it just has a space and not a '+' sign. So somewhere it's being changed. If you can't fix the code that changes it, you could replace that character with a space. That would mean, though, that you probably shouldn't accept '+' characters, because then you wouldn't know when one is real or an accident.

    On a side note, I notice that you're using OnGUI which isn't the best. You also have Application.LoadLevel which is deprecated, I believe. Older version maybe? Unrelated to your actual question, mind you.
     
    groch likes this.
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    It could also be the font itself, maybe.
     
    xVergilx and groch like this.
  8. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Yeah, I need to read about what's new in Unity, I was working on that game few years ago and now I'n not very well oriented. :)

    There is yet another script, but I assume that it's not that. ;)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HighScores : MonoBehaviour {
    5.  
    6.     const string privateCode = "wncWyq0ZFkObRFw5GkoBEgXRv1h_-YDEiCfVSTyGWGnQ";
    7.     const string publicCode = "55303d9e6e51b61f788cff38";
    8.     const string webURL = "http://dreamlo.com/lb/";
    9.  
    10.     public Highscore[] highscoresList;
    11.     static HighScores instance;
    12.     DisplayHighscores highscoresDisplay;
    13.  
    14.  
    15.     void Awake()
    16.     {
    17.         instance = this;
    18.         highscoresDisplay = GetComponent<DisplayHighscores>();
    19.     }
    20.  
    21.  
    22.     public static void AddNewHighscore(string username, int score)
    23.     {
    24.         instance.StartCoroutine (instance.UploadNewHighscore (username, score));
    25.     }
    26.  
    27.  
    28.     IEnumerator UploadNewHighscore(string username, int score)
    29.     {
    30.         WWW www = new WWW (webURL + privateCode + "/add/" + WWW.EscapeURL (username) + "/" + score);
    31.         yield return www;
    32.  
    33.         if (string.IsNullOrEmpty (www.error))
    34.         {
    35.             print ("Upload Successful");
    36.             DownloadHighscores();
    37.         }
    38.                 else
    39.                 {
    40.                     print ("Error Uploading: " + www.error);
    41.                 }
    42.     }
    43.  
    44.  
    45.  
    46.     public void DownloadHighscores()
    47.     {
    48.         StartCoroutine ("DownloadHighscoresFromDatabase");
    49.     }
    50.  
    51.  
    52.  
    53.  
    54.     IEnumerator DownloadHighscoresFromDatabase()
    55.     {
    56.         WWW www = new WWW (webURL + publicCode + "/pipe/");
    57.         yield return www;
    58.        
    59.         if (string.IsNullOrEmpty (www.error))
    60.         {
    61.             FormatHighscores (www.text);
    62.             highscoresDisplay.OnHighscoresDownloaded(highscoresList);
    63.         }
    64.         else
    65.         {
    66.             print ("Error Downloading: " + www.error);
    67.         }
    68.     }
    69.  
    70.     void FormatHighscores(string textStream)
    71.     {
    72.         string[] entries = textStream.Split (new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
    73.         highscoresList = new Highscore[entries.Length];
    74.         for (int i = 0; i <entries.Length; i ++)
    75.         {
    76.             string[] entryInfo = entries[i].Split(new char[] {'|'});
    77.             string username = entryInfo[0];
    78.             int score = int.Parse (entryInfo[1]);
    79.             highscoresList[i] = new Highscore(username,score);
    80.             print (highscoresList[i].username + ": " + highscoresList[i].score);
    81.         }
    82.     }
    83.  
    84. }
    85.  
    86. public struct Highscore
    87. {
    88.     public string username;
    89.     public int score;
    90.  
    91.     public Highscore(string _username, int _score)
    92.     {
    93.         username = _username;
    94.         score = _score;
    95.     }
    96. }
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I can't think of anything else beyond what's been said, sorry. Hope you get it sorted out. :)
     
    groch likes this.
  10. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Allright, thank you for your help methos5k. :)
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could try putting this in some test method to see what it gives you. Maybe it could rule out one thing or another?
    Code (csharp):
    1. string [] t = new string[] { "Rob|44", "George V|57", "Zeus|77", "Mickey Mike|101" };
    2. char c = '\n';
    3. string test = string.Join(c.ToString(),t);
    4. FormatHighscores(test);
    5. // you'd have to display these, too, but just an idea.
     
    groch likes this.
  12. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    When you add a score, you escape the username with EscapeURL:
    Code (csharp):
    1.  
    2. WWW www = new WWW (webURL + privateCode + "/add/" + WWW.EscapeURL (username) + "/" + score);
    3.  
    That's what is obliterating your spaces. You're pasting scores through an HTTP GET request, so you need to escape those characters to prevent all sorts of malicious and erroneous things.

    You may be able to use UnEscapeURL on the username before you display it to reverse the process:

    Code (csharp):
    1.  
    2. highscoreText[i].text += WWW.UnEscapeURL(highscoreList[i].username) + " - " + highscoreList[i].score;
    3.  
     
    groch likes this.
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Nice catch. :)
     
  14. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    I will try this and reply with the results, thanks a lot! :)