TextMeshPro input field OnValueChanged not passing string value

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:
6098457--662949--upload_2020-7-16_16-4-0.png

In the setter I am logging this:

    private string playerName = string.Empty;
    public string PlayerName {
        get
        {
            return playerName;
        }
        set
        {
            Debug.Log($"Setting player name to {value}.");
            playerName = value;
        }
    }

I typed my name in the field and saw 4 log statements:
6098457--662952--upload_2020-7-16_16-5-22.png

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

I tried mapping it to a function instead and see the same results:

    public void SetPlayerName(string name)
    {
        Debug.Log($"Setting player name to {name}.");
    }

What am I doing wrong?

The string parameter you have here is looking at the constant string value you can provide in the event UI in your first screenshot, which is empty. Check the example here: Unity - Scripting API: UI.InputField.onValueChanged

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:
6098979--663105--upload_2020-7-16_18-59-40.png

Here is the entire ChatNetworkManager class:

using UnityEngine;

namespace Mirror.Examples.Chat
{
    [AddComponentMenu("")]
    public class ChatNetworkManager : NetworkManager
    {
        public string PlayerName { get; set; }

        public void SetHostname(string hostname)
        {
            networkAddress = hostname;
        }

        public ChatWindow chatWindow;

        public class CreatePlayerMessage : MessageBase
        {
            public string name;
        }

        public override void OnStartServer()
        {
            base.OnStartServer();
            NetworkServer.RegisterHandler<CreatePlayerMessage>(OnCreatePlayer);
        }

        public override void OnClientConnect(NetworkConnection conn)
        {
            base.OnClientConnect(conn);

            // tell the server to create a player with this name
            conn.Send(new CreatePlayerMessage { name = PlayerName });
        }

        void OnCreatePlayer(NetworkConnection connection, CreatePlayerMessage createPlayerMessage)
        {
            // create a gameobject using the name supplied by client
            GameObject playergo = Instantiate(playerPrefab);
            playergo.GetComponent<Player>().playerName = createPlayerMessage.name;

            // set it as the player
            NetworkServer.AddPlayerForConnection(connection, playergo);

            chatWindow.gameObject.SetActive(true);
        }
    }
}

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:
6098979--663117--upload_2020-7-16_19-15-24.png

I added a Debug.Log statement to the Mirror chat example and I am getting values when I type in the field:

    [AddComponentMenu("")]
    public class ChatNetworkManager : NetworkManager
    {
        private string playerName;
        public string PlayerName
        {
            get
            {
                return playerName;
            }
            set
            {
                Debug.Log($"setting playerName to {value}");
                playerName = value;
            }
        }

6098997--663120--upload_2020-7-16_19-25-26.png

6098997--663123--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?

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.

5 Likes

For anyone else experiencing this same issue.

This thread helped a tiny bit in understanding: Getting string from InputField (On Value Changed())

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.

3 Likes

@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!

Thanks for solving the myth!!!

Babahahwhahhahhahajahhahahhahhshahahhahaja
Lol
You sooo right maaa men’s

@Grimshad , you’re a hero, thank you!