-1

I want to take username through InputField in scene 1 and access the username entered by the user in scene 2. I have a InputField and a submit button in scene 1. When click the submit button, the scene changes. How should I go about it?

I tried using DontDestroyOnLoad() but this didn't worked for me.

To change the scene i am using SceneManager.LoadScene(name);

  • have you tried using PlayerPrefs and Static Script, check this out -https://gamedev.stackexchange.com/questions/110958/unity-5-what-is-the-proper-way-to-handle-data-between-scenes – AgentP Apr 24 '19 at 05:51
  • @PraveenPanishetti: I tried using the PlayerPrefs but as I am a beginner I could not understand much in it. Could you please help me? – MD RAIHAN WALI Apr 24 '19 at 06:09
  • What was the problem with `DontDestroyOnLoad`? – Johnny Apr 24 '19 at 06:17
  • `DontDestroyOnLoad()` should work fine for the scenario you described. Are you sure you are using it correctly? – Jojofoulk Apr 24 '19 at 06:48
  • So you only want the Data to get Accessed ? Then you could just put it in a public Field and acces it with another Script in scene 2 – Assasin Bot Apr 24 '19 at 07:10
  • Please google before posting questions. This has been asked before. https://stackoverflow.com/questions/40078490/saving-loading-data-in-unity https://gamedev.stackexchange.com/questions/110958/unity-5-what-is-the-proper-way-to-handle-data-between-scenes – Saad Anees Apr 24 '19 at 07:57
  • Possible duplicate of [Saving/loading data in Unity](https://stackoverflow.com/questions/40078490/saving-loading-data-in-unity) – derHugo Apr 24 '19 at 12:01
  • Oh god, no. Do not pass data between scenes using `PlayerPrefs`! That is not what that system is for! With user an object with `DontDestroyOnLoad` or a static class that is always accessible. – Draco18s no longer trusts SE Apr 24 '19 at 15:20
  • Possible duplicate of [Unity - pass data between scenes](https://stackoverflow.com/questions/32306704/unity-pass-data-between-scenes) – Draco18s no longer trusts SE Apr 24 '19 at 15:21

1 Answers1

1

Here I came up with this.I achieved what you wanted with the using PlayerPrefs

I created a InputField and attached a script to it called Save and then I called SaveText() from that script after On End Event in inspector

Save.cs

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


public class Save : MonoBehaviour
{
  InputField userNameField;

   void Start()
   {
    userNameField = GetComponent<InputField>();
   }

   public void SaveText()
   {        
      PlayerPrefs.SetString("userName", userNameField.text);
      Debug.Log(userNameField.text);
   }

    public void MoveScene(string sceneToLoad)
   {
    SceneManager.LoadScene(sceneToLoad);
   }
}

and in next Scene I attached the script called Load.cs to a text gameobject and in Start() I got information of that String that saved previously

Load.cs

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

public class Load : MonoBehaviour
{

Text userNameText;

  void Start()
  {
    userNameText = GetComponent<Text>();
    userNameText.text = PlayerPrefs.GetString("userName");

}

}

Note : I don't know how efficient this is, But it does the work

AgentP
  • 6,261
  • 2
  • 31
  • 52
  • @MD RAIHAN WALI this is the easiest way of doing it since you are a beginner. However if you would like to see what are the different methods, read my comment above. – Saad Anees Apr 24 '19 at 07:58