Duplicate Question. Example: http://answers.unity3d.com/questions/36738/how-can-i-pass-data-between-two-levels.html
How do I save Entered Name and show it for the rest of the game?
absolute Unity beginner here. In a text game:
Start Screen says: "What is your name?" and there will be an InputField for Player to enter name.
How do I save that name and show it in the next scene? My InputName.class in C#:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class InputName : MonoBehaviour {
public static string username = "";
[SerializeField]
InputField input;
[SerializeField]
public Text NameDisplay;
public Text StartGame;
public void GetInput(string username){
Debug.Log ("Entered : "+username);
input.text= " ";
//username = input;
NameDisplay.text = ("Hello, " +username.ToString()+"!");
PlayerPrefs.GetString ("Username", username);
Debug.Log (username);
input.enabled = false;
DontDestroyOnLoad(this)
//Application.LoadLevel("Game2");
}
// Use this for initialization
//void Start () {
//NameDisplay.text = username;
//} */
//Update is called once per frame
void Update(){
if (input.enabled == false){
LetsStartGame();
}
}
void LetsStartGame(){
StartGame.text = "Press Space to Continue";
if (Input.GetKeyDown(KeyCode.Space))
Application.LoadLevel("Game2");
}
}
Once Player hits KeyCode.Space, Game2 will load. I would like to transfer the value from username variable into Game2 Scene. I tried the following code making username variable a static:
public class PlayerName : MonoBehaviour {
string usernameFromInputName;
//public class _inputName_inputName{
//public static string username;
//[SerializeField]
public Text text;
// Use this for initialization
void Start () {
//text.text= InputName.username;
}
// Update is called once per frame
void Update () {
text.text = InputName.username;
usernameFromInputName = InputName.username;
//print (InputName.username);
Debug.Log(InputName.username);
//PlayerPrefs.GetString (inputName.username);
}
}
There are no compiler errors but when Player enters their Name, their Name is not carried over to the next scene.
Hope my question makes sense and any help is appreciated.
Hi. You don't have to use [Help] on Unity Answers. It's obvious you need help when you ask a question. :)
It's a good habit to google your questions before asking new ones. That way we can avoid having hundreds of duplicate questions:
Additionally it saves you the time to write questions. ;)
Have a look at "Persistence" in Unity.
using DontDestroyOnLoad()
is one option you can quickly become used to.
Hi. Sorry for the duplicate question and thanks for your help. I did google but I got JavaScript tutorials or they use PlayerPrefs to save data (which I want to avoid).
As for the DontDestroyOnLoad(), I did try that. There were no compiler errors produced but the username's value from the InputName class did not transfer to the next scene.
Based on the videos I've watched, including the one you referred, I may need to rewrite a good chunk of this script?
(I'm not using multiple scenes, just one scene to another.)
I agree on you in that I should google and I will continue to keep searching. :)
If you can help me further on this, that would be great. If not, that's ok.
(I'm not using multiple scenes, just one scene to another.)
If you are loading the same scene multiple times, that is still using multiple scenes because loading a new scene erases everything changed in the current scene, just like the play mode in the editor.
If the entire game takes place in one scene, then you are not using multiple scenes, which erases the need for persistent things.
DontDestroyOnLoad() helps with that.
The one thing to note about it, is: When you have the same gameobject with the same scripts in the next scene, you will have to have it check for any duplicate of itself and disables itself so there are no duplicates prior to executing.
But that's covered in almost every DontDestroyOnLoad() related tutorial/post.
It's not neccessary to use PlayerPrefs, because you don't quit the application between setting the value and getting it.
Regarding your code, you might want to place DontDestroyOnLoad() in an early function like Start() or Awake().
PS: It's not really your fault for posting this. It's the fault of the moderator who chose to publish it. But, you should already have known the fundamental forum rules. ;)
Check out the FAQ and the User Guide in case you didn't.
You may have already found the solution by googling. I kind of late.
Follow this Question
Related Questions
StartCoroutine in a static member 0 Answers
Static bool 1 Answer
Switch-Case Statements and stateInfo.fullPathHash 1 Answer
When should I use static fields/methods? 2 Answers