In the first scene I ask the user for their name, and the name will be shown in the next scene like " Hi name" Code (CSharp): public class AskName : MonoBehaviour { public static AskName askName; public InputField inputField; public string userName; private void Awake() { if (askName == null) { askName = this; DontDestroyOnLoad(gameObject); } else Destroy(gameObject); } private void Update() { if (userName != inputField.text) { userName = inputField.text; } } } The class AskName is added to an inputField, and the class GetName is added to a text field in the next scene. Code (CSharp): public class GetName : MonoBehaviour { private Text nautName; public void Awake() { nautName = GetComponent<Text>(); nautName.text = AskName.askName.userName; } } The code works, and the name is shown, but there is an error and I don't understand what I have to do. NullReferenceException: Object reference not set to an instance of an object. GetName.Awake () (at Assets/Scripts/Main/GetName.cs:12) The problem seems to be with nautName.text = AskName.askName.userName;
Funny how I landed on the same 3-year-old post at the same time as you. At the moment I'm trying my best to find a solution but so far nothing has developed of my troubleshooting steps. If you ever solve the problem please let me know )
Dunno what the issue might be, did you make sure it does not accidentally destroy its own gameobject causing a null since it is part of monobehaviour maybe? Anyways a better solution could be to create a simple scriptableobject that only holds a string, it will be able to hold the string between scene transitions, that you can call upon when the name is needed, that is usually how I do it.
I did manage to kind of fix it, but i dont think its the best solution, as it only works on one other scene, not multiple. The name will be shown, but there will be an error again. The only thing I changed that so far worked is the GetName class. On the scene that you want the name to be shown, you create a new empty object, put the script inside the GameObject, and drag and drop the text field where the name will be shown. Hope this works for you as well and if you find another solution, please let me know Code (CSharp): public class GetName : MonoBehaviour { public GameObject textDisplay; public void Awake() { textDisplay.GetComponent<Text>().text = "Hi " + AskName.askName.userName; } }