1

I have a name input script in the first scene, the plan is I want to call this input in the second scene, when I enter the name in the first scene, then the name will appear in the second scene too, how do you do that?

public class NamaUser : MonoBehaviour {

    public InputField nama;

    public Text teks;

    public void NamaTeks () {
        if (nama.text == "") {
            teks.text = "Harap Isi Nama";
        } else {
            teks.text = "Namaku " + nama.text;
        }
    }
}
ahmad
  • 11
  • 2

2 Answers2

1

You can save the input's value PlayerPrefs.
Set the PlayerPrefs:

//Name of Pref in first parameter
//Value in second parameter
PlayerPrefs.SetString("value", teks.value);

Get the PlayerPref in second scene:

//Name of Pref in first parameter
//Returns value of PlayerPrefs
String a = PlayerPrefs.SetString("value");

Cons:

  1. You can pass data not only between scenes but also between instances (game sessions).
  2. Easy to manage since Unity handles all background process.
  3. Can be used to store data to track highscores.

Pros:

  1. Uses file system.
  2. Data can easily be changed from prefs file.
Ahmed Ali
  • 1,908
  • 14
  • 28
0

Or, another way -- use Singelton and DontDestroyOnLoad()

Allows easy access to fields and saves an object between scenes.

For example use this template, to create your class.

using UnityEngine;
public class Singelton<T> : MonoBehaviour where T : Singelton<T>
{

    private static T instance = null;

    private bool alive = true;

    public static T Instance
    {
        get
        {
            if (instance != null)
            {
                return instance;
            }
            else
            {
                //Find T
                T[] managers = GameObject.FindObjectsOfType<T>();
                if (managers != null)
                {
                    if (managers.Length == 1)
                    {
                        instance = managers[0];
                        DontDestroyOnLoad(instance);
                        return instance;
                    }
                    else
                    {
                        if (managers.Length > 1)
                        {
                            Debug.LogError($"Have more that one {typeof(T).Name} in scene. " +
                                            "But this is Singelton! Check project.");
                            for (int i = 0; i < managers.Length; ++i)
                            {
                                T manager = managers[i];
                                Destroy(manager.gameObject);
                            }
                        }
                    }
                }
                //create 
                GameObject go = new GameObject(typeof(T).Name, typeof(T));
                instance = go.GetComponent<T>();
                DontDestroyOnLoad(instance.gameObject);
                return instance;
            }
        }

        //Can be initialized externally
        set
        {
            instance = value as T;
        }
    }

    /// <summary>
    /// Check flag if need work from OnDestroy or OnApplicationExit
    /// </summary>
    public static bool IsAlive
    {
        get
        {
            if (instance == null)
                return false;
            return instance.alive;
        }
    }



    protected virtual void Awake()
    {
        if (instance == null)
        {
            DontDestroyOnLoad(gameObject);
            instance = this as T;
        }
        else
        {
            Debug.LogError($"Have more that one {typeof(T).Name} in scene. " +
                            "But this is Singelton! Check project.");
            Destroy(gameObject);
        }

    }

    protected virtual void OnDestroy() { alive = false; }

    protected virtual void OnApplicationQuit() { alive = false; }
}

Example of using:

class MyClass Settings : Singelton<Settings>
{
   string param;
}