Passing InputField Text to Next Scene

Hi,

I have basically drawn a diagram of what I want to happen xD, I have attempted it myself however I could get it to work.

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

public class save_names : MonoBehaviour {
   
    public static string userName;
    public GameObject inputField;

    public void UserName()
    {
        userName = inputField.GetComponent<InputField>().text;

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class display_inputs : MonoBehaviour {

    private Text nautName;

    void Start ()
    {
        nautName = gameObject.GetComponent<Text>();

        save_names.userName = nautName.text;
    }

You could make a singlton out of save names, if you do it this way I recommend storing all save data in a single object

using UnityEngine;
using UnityEngine.UI;

public class save_names : MonoBehaviour {

    public static save_names save_Names;
    public InputField inputField;
    public string userName;

    private void Awake()
    {
        if (save_Names == null)
        {
            save_Names = this;
            DontDestroyOnLoad(gameObject);
        }
        else Destroy(gameObject);
    }

    private void Update()
    {
        if (userName != inputField.text)
        {
            userName = inputField.text;
        }
    }
}

Then you can call it from any script:

string name = save_names.save_Names.userName;

Thanks for the reply,

I put the save_names script on a empty game object and dragged the input field into the variable. The other script I put on the Text field, which is where the name should appear that was typed in.

Not entirely sure if I have set it up because it doesn’t seem to change the name when I go into the game scene.

Here is the script accessing the static variable in the game scene (based off what you have said above):

public class display_inputs : MonoBehaviour {

    public Text nautName;

    public void Awake ()
    {
        string nautName = save_names.save_Names.userName;
    }
}

You’d probably want to set it like this:

    public class display_inputs : MonoBehaviour
    {

        private Text nautName;

        public void Awake()
        {
            nautName = GetComponent<Text>();
            nautName.text = save_names.save_Names.userName;
        }
    }
1 Like

Aaaah man! Thanks a bunch! It works, really appreciate the help bro!!! :slight_smile:

Dean

1 Like

Hi. It does work, but it shows an error:
NullReferenceException: Object reference not set to an instance of an object.

And I really dont know what to do

samer error for me too