- Home /
Get Text from an Input Field on one scene and use it as a text on another scene Unity c#
So I have a script where I get text from an input field and when I start the game the text appears in the game as a "NickName" feature. I have almost gotten it but there is one thing left to my knowledge that i need to fix. Here is my script:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class PlayerName : MonoBehaviour { public string Name;
public Transform Nick;
public GameObject name1;
public Transform SaveData;
public Transform Canvas;
public Button start;
public bool IsInGame = false;
void Start ()
{
DontDestroyOnLoad(SaveData);
DontDestroyOnLoad(Canvas);
Name = Nick.GetComponent<InputField>().text;
}
void Update ()
{
Name = Nick.GetComponent<InputField>().text;
if(IsInGame == true)
{
Debug.LogError("You are in a game");
name1 = transform.Find("NickName").gameObject;
name1.GetComponent<Text>().text = Name;
if (transform.Find("NickName") != null)
{
Debug.Log("The Editor has found the object 'Nickname'.");
}
else
{
Debug.LogError("The editor cannot find the object 'Nickname'.");
}
}
if(IsInGame == false)
{
Debug.LogError("you are not in a game");
}
}
}
(IsInGame is true when my Script StartGame script is activated)
my problem inlies that it cannot find the transform NickName. I get a nullReferenceExeption error and the LogError to catch if the transform is null does not appear in the Console.
Thank You for helping me!
Your answer
Follow this Question
Related Questions
Prevent focus from leaving inputfield 0 Answers
Text.text doesnt work after scene change 2 Answers
Inputfield text to String variable 1 Answer
Previous UI text sticks around even after reloading scene using SceneManager.LoadScene(scene.name); 0 Answers
Problems in the script for making a text disappear after few seconds 2 Answers