I’m developing a Unity game. I have a login scene in the game that fetches user information from the database. After fetching the user information, a model class which is named as RegisteredUser is used to hold the user credentials. I need to send the created RegisteredUser class object to the next scene. But I cannot send it directly. When I try it, I get NullExceptionError and the object is lost. How can I fix it? This is the callback function that user credentials are returned:
public void userInfo (RegisteredUser newUser)
{
loggedinUser.FirstName = newUser.FirstName;
loggedinUser.LastName = newUser.LastName;
loggedinUser.Email = newUser.Email;
loggedinUser.Gender = newUser.Gender;
}
And this is the part where I want to fetch the name and surname of the user to print on the text field:
void Start()
{
// string userName = currentUser.FirstName currentUser.LastName;
welcomeText = GameObject.Find("Canvas/SidebarPanel/WelcomeText").GetComponent<Text>();
}
CodePudding user response:
Store your object in a field/property of a MonoBehaviour script that is marked DontDestroyOnLoad.
This object will persist and so will its data.
public class PersistentDataContainer : MonoBehaviour
{
public RegisteredUser UserInfo;
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
}