Search Unity

Resolved TextMeshPro input field OnValueChanged not passing string value

Discussion in 'Editor & General Support' started by MidniteOil, Jul 16, 2020.

  1. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    Greetings,

    I have a TextMeshPro input field in a scene and I've mapped the OnValueChanged event to a property on the mirror NetworkRoomManagerExt class:
    upload_2020-7-16_16-4-0.png

    In the setter I am logging this:
    Code (CSharp):
    1.     private string playerName = string.Empty;
    2.     public string PlayerName {
    3.         get
    4.         {
    5.             return playerName;
    6.         }
    7.         set
    8.         {
    9.             Debug.Log($"Setting player name to {value}.");
    10.             playerName = value;
    11.         }
    12.     }
    I typed my name in the field and saw 4 log statements:
    upload_2020-7-16_16-5-22.png

    In every one there was no string value:
    upload_2020-7-16_16-5-41.png

    I tried mapping it to a function instead and see the same results:
    Code (CSharp):
    1.  
    2.     public void SetPlayerName(string name)
    3.     {
    4.         Debug.Log($"Setting player name to {name}.");
    5.     }
    What am I doing wrong?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Last edited: Oct 21, 2022
  3. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    Thanks for taking the time to reply to my question.

    Based on your answer, I'm confused as to how the Chat example provided with the Mirror package works.
    In their example. they have a TMPro Input field and they map the OnValueChanged event to the PlayerName property on the ChatNetworkManager class:
    upload_2020-7-16_18-59-40.png

    Here is the entire ChatNetworkManager class:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Mirror.Examples.Chat
    4. {
    5.     [AddComponentMenu("")]
    6.     public class ChatNetworkManager : NetworkManager
    7.     {
    8.         public string PlayerName { get; set; }
    9.  
    10.         public void SetHostname(string hostname)
    11.         {
    12.             networkAddress = hostname;
    13.         }
    14.  
    15.         public ChatWindow chatWindow;
    16.  
    17.         public class CreatePlayerMessage : MessageBase
    18.         {
    19.             public string name;
    20.         }
    21.  
    22.         public override void OnStartServer()
    23.         {
    24.             base.OnStartServer();
    25.             NetworkServer.RegisterHandler<CreatePlayerMessage>(OnCreatePlayer);
    26.         }
    27.  
    28.         public override void OnClientConnect(NetworkConnection conn)
    29.         {
    30.             base.OnClientConnect(conn);
    31.  
    32.             // tell the server to create a player with this name
    33.             conn.Send(new CreatePlayerMessage { name = PlayerName });
    34.         }
    35.  
    36.         void OnCreatePlayer(NetworkConnection connection, CreatePlayerMessage createPlayerMessage)
    37.         {
    38.             // create a gameobject using the name supplied by client
    39.             GameObject playergo = Instantiate(playerPrefab);
    40.             playergo.GetComponent<Player>().playerName = createPlayerMessage.name;
    41.  
    42.             // set it as the player
    43.             NetworkServer.AddPlayerForConnection(connection, playergo);
    44.  
    45.             chatWindow.gameObject.SetActive(true);
    46.         }
    47.     }
    48. }
    49.  
    I don't see anyplace in there where they get the value from the input field. Yet somehow, the name entered into the Input field is assigned to the PlayerName property and subsequently sent to the player GameObject when it's instantiated:
    upload_2020-7-16_19-15-24.png
     
  4. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    I added a Debug.Log statement to the Mirror chat example and I am getting values when I type in the field:
    Code (CSharp):
    1.     [AddComponentMenu("")]
    2.     public class ChatNetworkManager : NetworkManager
    3.     {
    4.         private string playerName;
    5.         public string PlayerName
    6.         {
    7.             get
    8.             {
    9.                 return playerName;
    10.             }
    11.             set
    12.             {
    13.                 Debug.Log($"setting playerName to {value}");
    14.                 playerName = value;
    15.             }
    16.         }
    upload_2020-7-16_19-25-26.png

    upload_2020-7-16_19-26-8.png .

    I just noticed that the text input field in the Mirror Chat example is not a TextMeshPro text input field. Would TMPro really have changed the OnValueChanged event handler to not send a string parameter?
     
  5. Ugindie

    Ugindie

    Joined:
    Jul 10, 2013
    Posts:
    5
    This is wrong, from the unity UI, you can select between a function call with a static input or dynamic input when setting your onValueChanged callback. Please don't mislead folks here.
     
  6. Grimshad

    Grimshad

    Joined:
    Dec 28, 2011
    Posts:
    9
    For anyone else experiencing this same issue.

    This thread helped a tiny bit in understanding: https://forum.unity.com/threads/getting-string-from-inputfield-on-value-changed.746636/

    But in the end I figured it out on my own. It took me way too long, but if you look in the functions list there's actually 2 sections and they have the same functions in each section. The top section is the Dynamic section that passes the string where as the bottom section is the Static section that does not pass the string.

    Ugindie touched on this, but didn't actually provide any explanation or help.
     
  7. rlalancette

    rlalancette

    Joined:
    Sep 20, 2014
    Posts:
    18
    @Grimshad You are a life saver. I was stuck on this for about half an hour, haven't realized the function was in both spots. Thank you!
     
  8. sunwangshu

    sunwangshu

    Joined:
    Mar 2, 2017
    Posts:
    21
    Thanks for solving the myth!!!
     
  9. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
    Babahahwhahhahhahajahhahahhahhshahahhahaja
    Lol
    You sooo right maaa men's
     
  10. hestermancarter

    hestermancarter

    Joined:
    Feb 12, 2023
    Posts:
    1