Why is inputfield.text returning empty?

The update loop returns the text that I input into the field and updates it each frame but when I click the button associated with the LogIn method it sends an empty string even if the debug statement doesn’t show an empty string.

4671026--439592--Bug.png

This is fine for now, but in the future please use code tags and simply copy and paste the code into the insert → code window instead. This is significantly faster and allows us to just view the code inline instead of opening a separate tab/window, which may be annoying on mobile devices. Being able to select and copy/paste code is better if we need to post corrections too, so we aren’t forced to write something from scratch while eyeballing your screenshot.

Please Debug.Log the username.text from OnConnectPressed directly, instead of relying on some log in Network.LogIn(), so we can be absolutely sure the problem is happening in this script and not further in.

How is the Button component associated with this script? Have you drag-and-dropped the Login script reference into the Button’s event handler in the UI hierarchy, or are you accessing it through the singleton instance? Either way, it may not be pointing to the same object instance that’s running the Update method and is receiving the input text from the user. I would suggest debug.logging the object instance ID to the console, from Update and OnConnectPressed, and make sure they match, especially if your singleton setup involves automatic instantiation of any kind when an instance doesn’t exist.

Keep in mind that Update only runs on enabled components on enabled GameObjects, while you can still run any function on any component if you reference and access them directly. It’s possible that the duplicate Login script simply isn’t enabled in the scene, or doesn’t exist as a scene object, explaining why the empty string isn’t being logged along with the non-empty version from the other instance (because Update isn’t actually being run on it).

Can you post your singleton code as well?

I dragged and dropped the Login script reference into the button’s event handler in the UI hiearchy.

Here is the singleton code

private static Login instance;
public static Login Instance { get { return instance; } }

private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}

instance = this;
}

Currently I got it working by assigning the button to the Login script via a serialized field and adding the OnConnectPressed method by saying button.OnClick.AddListener(OnConnectPressed);

Yeah, it definitely seems like it was pointing to a wrong instance then- there really isn’t much difference between assigning the delegate in Start on the Login side (using a drag-and-dropped reference to the Button component, or GetComponentInChildren, or whatever), or using the inspector to assign the delegate to the event handler on the Button side. Both should work equally well.

If you’re fine just leaving it this way, it’s perfectly valid methodology. If you want to get it working from the other side instead, you’ll have to undo your fix because it just complicates the issue. Let me know. If the latter, please debug log the instance ID to be sure it’s using the same instance in both cases, it’s really the only thing I can think of that accounts for the text not being identical for each function call.