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

Help needed with Input Field

Discussion in 'Scripting' started by groch, May 14, 2015.

  1. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Hello,

    I have an online highscore script, where the player have to type in his nickname using Input Field after game over. For some reason, after typing player's nickname in that Input Field and clicking "save score", the script saves it without last letter.

    What might be the reason?



    Scripts:

    1:

    Code (CSharp):
    1.  
    2. Text userNameInputText;
    3.  
    4.   string playerName = "";
    5.  
    6.     public void InputYourName()
    7.     {
    8.         this.playerName = userNameInputText.text;
    9.     }
    10.  
    11.   void Start ()
    12.    {
    13.   instance = this;
    14.   totalScore = 0;
    15.  
    16.   userNameInputText = canvas.transform.Find("InputField/Text").GetComponent<Text>();
    17.  
    18.              // get the reference here...
    19.              this.dl = dreamloLeaderBoard.GetSceneDreamloLeaderboard ();
    20.  
    21.   this.gs = gameState.leaderboard;
    22.    }
    23.  
    2:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class dreamloLeaderBoard : MonoBehaviour {
    6.    
    7.     string dreamloWebserviceURL = "http://dreamlo.com/lb/";
    8.    
    9.     public string privateCode = "it's a secret :)";
    10.     public string publicCode = "public code goes here";
    11.    
    12.     string highScores = "";
    13.    
    14.     ////////////////////////////////////////////////////////////////////////////////////////////////
    15.    
    16.     // A player named Carmine got a score of 100. If the same name is added twice, we use the higher score.
    17.      // http://dreamlo.com/lb/(your super secret very long code)/add/Carmine/100
    18.  
    19.     // A player named Carmine got a score of 1000 in 90 seconds.
    20.      // http://dreamlo.com/lb/(your super secret very long code)/add/Carmine/1000/90
    21.    
    22.     // A player named Carmine got a score of 1000 in 90 seconds and is Awesome.
    23.      // http://dreamlo.com/lb/(your super secret very long code)/add/Carmine/1000/90/Awesome
    24.    
    25.     ////////////////////////////////////////////////////////////////////////////////////////////////
    26.    
    27.    
    28.     public struct Score {
    29.         public string playerName;
    30.         public int score;
    31.         public int seconds;
    32.         public string shortText;
    33.         public string dateString;
    34.     }
    35.    
    36.     void Start()
    37.     {
    38.         this.highScores = "";
    39.     }
    40.    
    41.     public static dreamloLeaderBoard GetSceneDreamloLeaderboard()
    42.     {
    43.         GameObject go = GameObject.Find("dreamloPrefab");
    44.        
    45.         if (go == null)
    46.         {
    47.             Debug.LogError("Could not find dreamloPrefab in the scene.");
    48.             return null;
    49.         }
    50.         return go.GetComponent<dreamloLeaderBoard>();
    51.     }
    52.    
    53.     public void AddScore(string playerName, int totalScore)
    54.     {
    55.         StartCoroutine(AddScoreWithPipe(playerName, totalScore));
    56.     }
    57.    
    58.     public void AddScore(string playerName, int totalScore, int totalSeconds)
    59.     {
    60.         StartCoroutine(AddScoreWithPipe(playerName, totalScore, totalSeconds));
    61.     }
    62.    
    63.     public void AddScore(string playerName, int totalScore, int totalSeconds, string shortText)
    64.     {
    65.         StartCoroutine(AddScoreWithPipe(playerName, totalScore, totalSeconds, shortText));
    66.     }
    67.    
    68.     // This function saves a trip to the server. Adds the score and retrieves results in one trip.
    69.     IEnumerator AddScoreWithPipe(string playerName, int totalScore)
    70.     {
    71.         playerName = Clean(playerName);
    72.        
    73.         WWW www = new WWW(dreamloWebserviceURL + privateCode + "/add-pipe/" + WWW.EscapeURL(playerName) + "/" + totalScore.ToString());
    74.         yield return www;
    75.         highScores = www.text;
    76.     }
    77.    
    78.     IEnumerator AddScoreWithPipe(string playerName, int totalScore, int totalSeconds)
    79.     {
    80.         playerName = Clean(playerName);
    81.        
    82.         WWW www = new WWW(dreamloWebserviceURL + privateCode + "/add-pipe/" + WWW.EscapeURL(playerName) + "/" + totalScore.ToString()+ "/" + totalSeconds.ToString());
    83.         yield return www;
    84.         highScores = www.text;
    85.     }
    86.    
    87.     IEnumerator AddScoreWithPipe(string playerName, int totalScore, int totalSeconds, string shortText)
    88.     {
    89.         playerName = Clean(playerName);
    90.         shortText = Clean(shortText);
    91.        
    92.         WWW www = new WWW(dreamloWebserviceURL + privateCode + "/add-pipe/" + WWW.EscapeURL(playerName) + "/" + totalScore.ToString() + "/" + totalSeconds.ToString()+ "/" + shortText);
    93.         yield return www;
    94.         highScores = www.text;
    95.     }
    96.    
    97.     IEnumerator GetScores()
    98.     {
    99.         highScores = "";
    100.         WWW www = new WWW(dreamloWebserviceURL +  publicCode  + "/pipe");
    101.         yield return www;
    102.         highScores = www.text;
    103.     }
    104.    
    105.     IEnumerator GetSingleScore(string playerName)
    106.     {
    107.         highScores = "";
    108.         WWW www = new WWW(dreamloWebserviceURL +  publicCode  + "/pipe-get/" + WWW.EscapeURL(playerName));
    109.         yield return www;
    110.         highScores = www.text;
    111.     }
    112.    
    113.     public void LoadScores()
    114.     {
    115.         StartCoroutine(GetScores());
    116.     }
    117.  
    118.    
    119.     public string[] ToStringArray()
    120.     {
    121.         if (this.highScores == null) return null;
    122.         if (this.highScores == "") return null;
    123.        
    124.         string[] rows = this.highScores.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
    125.         return rows;
    126.     }
    127.    
    128.     public List<Score> ToListLowToHigh()
    129.     {
    130.         Score[] scoreList = this.ToScoreArray();
    131.        
    132.         if (scoreList == null) return new List<Score>();
    133.        
    134.         List<Score> genericList = new List<Score>(scoreList);
    135.            
    136.         genericList.Sort((x, y) => x.score.CompareTo(y.score));
    137.        
    138.         return genericList;
    139.     }
    140.    
    141.     public List<Score> ToListHighToLow()
    142.     {
    143.         Score[] scoreList = this.ToScoreArray();
    144.        
    145.         if (scoreList == null) return new List<Score>();
    146.  
    147.         List<Score> genericList = new List<Score>(scoreList);
    148.            
    149.         genericList.Sort((x, y) => y.score.CompareTo(x.score));
    150.        
    151.         return genericList;
    152.     }
    153.    
    154.     public Score[] ToScoreArray()
    155.     {
    156.         string[] rows = ToStringArray();
    157.         if (rows == null) return null;
    158.        
    159.         int rowcount = rows.Length;
    160.        
    161.         if (rowcount <= 0) return null;
    162.        
    163.         Score[] scoreList = new Score[rowcount];
    164.        
    165.         for (int i = 0; i < rowcount; i++)
    166.         {
    167.             string[] values = rows[i].Split(new char[] {'|'}, System.StringSplitOptions.None);
    168.            
    169.             Score current = new Score();
    170.             current.playerName = values[0];
    171.             current.score = 0;
    172.             current.seconds = 0;
    173.             current.shortText = "";
    174.             current.dateString = "";
    175.             if (values.Length > 1) current.score = CheckInt(values[1]);
    176.             if (values.Length > 2) current.seconds = CheckInt(values[2]);
    177.             if (values.Length > 3) current.shortText = values[3];
    178.             if (values.Length > 4) current.dateString = values[4];
    179.             scoreList[i] = current;
    180.         }
    181.        
    182.         return scoreList;
    183.     }
    184.    
    185.    
    186.    
    187.     // Keep pipe and slash out of names
    188.    
    189.     string Clean(string s)
    190.     {
    191.         s = s.Replace("/", "");
    192.         s = s.Replace("|", "");
    193.         return s;
    194.        
    195.     }
    196.    
    197.     int CheckInt(string s)
    198.     {
    199.         int x = 0;
    200.        
    201.         int.TryParse(s, out x);
    202.         return x;
    203.     }
    204.    
    205. }
    206.  
     
    Last edited: May 15, 2015
  2. Elis_Sokolowski

    Elis_Sokolowski

    Joined:
    Mar 18, 2013
    Posts:
    3
    Hi Groch,

    There doesn't seem to be anything blindingly obvious in your code that would cause this, what I would recommend is just adding Debug.Log(playerName) in each function along the chain, and check where it changes, are you sure this is immediately after you save it or could it be when it is sent to the display?

    Also, Line 167 of #2 I'm pretty sure you want that to be - System.StringSplitOptions.RemoveEmptyEntries not .None, Because at the moment you will be trying to parse "" to some of your current variables.

    If you can provide any more information I'll be happy to respond to it below,

    Elis
     
    groch likes this.
  3. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Thank you for your reply, for now I've discovered that when using an old Text Field (old GUI Layout) then it's ok. I've added both Input Field and old Text Field and when I type the first letter in Input Field, then there's nothing in Text Field and after typing second letter, then my text starts to appear in Text Field but without last letter. I hope that I've described it clearly, strange thing. Now I am struggling with centering that old Text Field... :d

    EDIT:

    Ok, I've done everything I need now, I am using that old Text Field and it's also centered.
     
    Last edited: May 15, 2015
    Elis_Sokolowski likes this.