Search Unity

Pass InputField to next Scene

Discussion in '2D' started by ZoiGkatsi, May 24, 2020.

  1. ZoiGkatsi

    ZoiGkatsi

    Joined:
    May 2, 2020
    Posts:
    3
    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):
    1. public class AskName : MonoBehaviour
    2. {
    3.     public static AskName askName;
    4.     public InputField inputField;
    5.     public string userName;
    6.  
    7.  
    8.     private void Awake()
    9.     {
    10.         if (askName == null)
    11.         {
    12.             askName = this;
    13.             DontDestroyOnLoad(gameObject);
    14.         }
    15.         else Destroy(gameObject);
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (userName != inputField.text)
    21.         {
    22.             userName = inputField.text;
    23.         }
    24.     }
    25. }
    The class AskName is added to an inputField, and the class GetName is added to a text field in the next scene.

    Code (CSharp):
    1. public class GetName : MonoBehaviour
    2. {
    3.     private Text nautName;
    4.     public void Awake()
    5.     {
    6.         nautName = GetComponent<Text>();
    7.         nautName.text = AskName.askName.userName;
    8.     }
    9. }

    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;
     
  2. LimeRsj

    LimeRsj

    Joined:
    May 25, 2020
    Posts:
    4
    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 :))
     
  3. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    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.
     
  4. ZoiGkatsi

    ZoiGkatsi

    Joined:
    May 2, 2020
    Posts:
    3
    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):
    1. public class GetName : MonoBehaviour
    2. {
    3.     public GameObject textDisplay;
    4.  
    5.     public void Awake()
    6.     {
    7.         textDisplay.GetComponent<Text>().text = "Hi " + AskName.askName.userName;
    8.     }
    9. }
     
  5. LimeRsj

    LimeRsj

    Joined:
    May 25, 2020
    Posts:
    4
    Thanks for the reply! I'll also make sure to inform you if I find something better :)))
     
  6. LimeRsj

    LimeRsj

    Joined:
    May 25, 2020
    Posts:
    4
    Ok I'll check it out. Thanks for the advice