Trying to save a userName variable from an InputField to another Script.

Hi! I’ve been looking around to find a way to save a InputField entry (as a username) into another script, but I did not find or understand how to do it.

From now, I got 2 objects, 1 InputField and 1 EmptyObject (UI Controller).

I set up the InputField to have the Ui Controller into the On End Edit. I selected the script userNameScript, wich I have attached to my UI Controller, and finally, selected the fonction OnEntry(), wich I have written.

My OnEntry() goes like this :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class userNameScript : MonoBehaviour {

	public string userName;
	public GameObject inputField;

	public void OnEntry()
	{
		inputField.GetComponent<InputField>().text = userName;
		Debug.Log ("your username is " + userName);
	}
}

My Debug Log returns nothing right now, as the userName is still empty and did not receives the text from the InputField.

Where did I go wrong?

Thanks a lot for you help!!

You have your assignment backwards, whats on the LEFT side of the = sign, is saying you want that TO BE whatever the right side is… So right now your variable “userName”, which is never set to anything at any point, your setting your input field’s text to be that, instead of the other way around. You just need to flip the operation around so it reads userName = inputField.GetComponent<InputField>().text;

Which is now saying “make userName = to whatever inputField’s text does”.