-1

I have started today with some programming in C# with Unity. How can I get the content of a value from the 1st_Scene to the 2nd_Scene?

In the first scene, I have to recognize a picture with the Vuforia engine. After the picture is identified, you can see on a canvas an input Textfield, the Button "Transfer", an empty Textfield and a second button "Next". First I write my name in the Textfield. With a click on the first button "Transfer", the text is displayed in the empty Textfield. Then I click on the second button "Next". This will load the 2nd_scene. Additionally the variable from the first Textfield should be displayed in the second scene, also within a Textfield in the canvas in the 2nd scene, but it won't.

I have tried to do this with a static variable and an extra static class. But Unity shows always this error:

[Error message shown by Unity

This is the script.

public class Button_Event : MonoBehaviour {
    public InputField fInput;
    public Text txtEingabe;

    public void Setget() {
        txtEingabe.text = "Your name is: " + fInput.text;
    }

    public void SwitchScene() {
        DontDestroyOnLoad(this.gameObject); 
        SceneManager.LoadScene(1, LoadSceneMode.Single);
        txtEingabe.text = "Your name is: " + fInput.text;
    }
}

Here is picture of the Objects in the two scenes: [Picture of the two scenes

2 Answers2

2

While DontDestroyOnLoad will allow your gameObject to persist across scenes, it won't be easily accessible to other components in your new scene without making it a singleton. The singleton pattern is useful, but for pure data transfer I recommend looking at ScriptableObjects. They function more like pointers. Here's a breakdown:

A ScriptableObject holds data outside of scene scope. As a result of a ScriptableObject persisting outside of the scene, a reference to it acts similar to a pointer. If one scene places data on the ScriptableObject and promptly ends (i.e. the scene changes), the ScriptableObject still persists the data that was placed on it. If a new scene is started and a new object is reading from that same ScriptableObject, then that new scene can read that data that was placed there.

Here's a good example use case:

[CreateAssetMenu(fileName = "PlayerData", menuName = "Player Data", order = 1)]
public class PlayerData : ScriptableObject 
{
    public string name;
}
public class NameInput : Monobehaviour
{
    public InputField fInput;
    public PlayerData playerData;

    public void NameSubmission()
    {
        playerData.name = fInput.text;
    }
}
public class Character : Monobehaviour
{
    public PlayerData playerData;

    public void Start()
    {
        Debug.Log(playerData.name);
    }
}

When you create a PlayerData ScriptableObject in your assets folder, you can then drag it onto any Character or NameInput GameObject's inspectors in your scene. The data now lives inside of the PlayerData asset object, rather than inside the scope of the scene. Please note that this does not persist the data outside of runtime, so you cannot close and restart the game and expect the name to persist.

Erik Overflow
  • 2,220
  • 1
  • 5
  • 16
0

What you are looking for is https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Just call your this function on you object(the one that the script is attached), and that script and it's values will stay the same.

Milos Romanic
  • 457
  • 3
  • 12
  • I'am verry sorry, but I don't understand, what you are trying to say. I create a new script and add the excample of the code in the link. What do I have to do afterwards? – Rapha W. Oct 03 '19 at 13:28
  • 3
    @milosromantic asker is already doing that – Ruzihm Oct 03 '19 at 13:36