Stay logged in, in different scene unity

Hello guys.

I’m making a register/login system in unity.I’ve managed to do it and the user can register and log in normally and start the next scene.
The problem is in the next scene the user doesn’t have his status(username etc…).
How can I do to acces to this information on the other scene ? Thank you.

You need to store it. Either in a singleton for user data or static variables, that way you can access the data stored there from other scenes.

sorry, but i am totally new to this. how can i stor it in a static variables ?

here is my code for my login page

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql.Data.MySqlClient;
using UnityEngine.UI;
using System.IO;

public class DataBaseManager : MonoBehaviour {
    public string host;
    public string database;
    public string username;
    public string password;
    public Text TxtState;
    MySqlConnection con;
    public InputField IfLogin;
    public InputField IfPassword;
    public Text TxtLogin;
    public string DataString;



    struct _Player
    {
        public int ID;
        public string Pseudo;
        public string Password;
        public int Point;
    }

    _Player Player;

    //
    public static DataBaseManager _Instance;

    public DataBaseManager Script;

    void Awake()
    {
        DontDestroyOnLoad(this);
    }


  

 



    void ConnectBDD () {
     
        string constr = "Server=" + host + ";DATABASE=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=true;Charset=utf8;";

        try
        {
            con = new MySqlConnection(constr);  
            con.Open();                           
            TxtState.text = con.State.ToString();
        }
        catch(IOException e)
        {
            TxtState.text = e.ToString();
        }

      
    }
  


    void OnApplicationQuit()
    {
        Debug.Log("fermeture Connection");

        if (con != null && con.State.ToString()!="Closed")
            {
            con.Close(); // fermer
        }
    }

    public void Register()
    {
        ConnectBDD();
        bool Exist = false;

   
        MySqlCommand commandsql = new MySqlCommand("SELECT pseudo FROM users WHERE pseudo ='" + IfLogin.text + "'",con);
        MySqlDataReader MyReader = commandsql.ExecuteReader();

        while (MyReader.Read())
        {
            if(MyReader["pseudo"].ToString() != "")
            {
                TxtLogin.text = "Login already exist";
                Exist = true;
            }

        }
        MyReader.Close();
        if (!Exist)
        {
            string command = "INSERT INTO users VALUES(default,'" + IfLogin.text + "','" + IfPassword.text + "','')";
            MySqlCommand cmd = new MySqlCommand(command, con);  

            try
            {
                cmd.ExecuteReader(); 
                TxtLogin.text = "Register successfull";
            catch (IOException e)
            {
                TxtState.text = e.ToString();

            }

            cmd.Dispose();
            con.Close();
        }

      
    }


    public void Login(string scenename)
    {
        ConnectBDD();
        string pass = null;
        try
        {

            MySqlCommand commandesql = new MySqlCommand("SELECT * FROM users WHERE pseudo ='" + IfLogin.text + "'", con);
            MySqlDataReader Myreader = commandesql.ExecuteReader();
            while (Myreader.Read())
            {

                pass = Myreader["password"].ToString();
                if (pass == IfPassword.text)
                {
                    Player.ID = (int)Myreader["ID"];
                    Player.Pseudo = Myreader["pseudo"].ToString();
                    Player.Password = Myreader["password"].ToString();
                    Player.Point = (int)Myreader["point"];
                    TxtLogin.text = "Number of clics : " + Player.Point;
                    DataString = Myreader["pseudo"].ToString();
                    UnityEngine.SceneManagement.SceneManager.LoadScene(scenename);

                }
                else
                {
                    TxtLogin.text = "Invalid pseudo or password";
                }
            }

            if (pass==null)
            {
                TxtLogin.text = "Your account does not exist";
            }
            Myreader.Close();


        }
        catch (IOException e)
        {
            TxtState.text = e.ToString();
        }

        con.Close();
    }

    public void Point()
    {
        ConnectBDD();
      
        Player.Point++;
        string command = "UPDATE users SET point='" + Player.Point + "' WHERE pseudo='" + Player.Pseudo + "';";
        MySqlCommand cmd = new MySqlCommand(command, con);
        try
        {
            cmd.ExecuteReader();
            TxtLogin.text = "Succès";
        }
        catch(IOException e)

        {
            TxtState.text = e.ToString();
        }
        cmd.Dispose();
        con.Close();
    }

}

You could make data container class for your session and player data and store them in to a Toolbox. I’ve written a simple ToolBox example here that details with GameManager how you can make cross-scene MonoBehaviors and non-behaviors with SaveSystem.

Alternatively you can check out more complex version of a Toolbox from Unity Wiki.

Toolbox is basically a singleton for storing references, so it persist between scenes. The Gameobjects attached to Behaviors that are stored in toolbox should call GameObject.DontDestroyOnLoad to avoid getting destroyed during scene transition.

static is pretty simple, just use it correctly. Just remember static is shared, so you wouldn’t want for example static enemy health as all enemies using the same code would share the same health. I would suggest you research singletons and static before you start using it as there is some things to keep in mind about them.

this:
public string username;

becomes this:
public static string username;

Then you access username with
DataBaseManager.username