hi programmers and friend. first of all sorry for my bad english/grammer. i started with Unity 1 week ago and i enjoy it when im developing games. i started with the Roll A Ball tutorial. and i made my own levels. i was thinking to make a log in screen and i made it. i used MSSQL(because i have experience with it) and the log in page succeed but i also want that if u log in with a specific name that it saves the username in another script so i can use it in the levels to save scores in their own database row.
my question is: is there a way how i can save the username which i use to log in to save in another script which i can use to save highscore in my database.
here are some codes and evidence:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;
public class Login : MonoBehaviour {
public GameObject username;
public GameObject password;
public Text errormessage;
private string Username;
private string Password;
private string Form;
void Start () {
errormessage.text = "";
}
void Update () {
Username = username.GetComponent<InputField>().text;
Password = password.GetComponent<InputField>().text;
if (Input.GetKeyDown(KeyCode.Tab))
{
if(username.GetComponent<InputField>().isFocused)
{
password.GetComponent<InputField>().Select();
}
}
if (username.GetComponent<InputField>().text != "" &&
password.GetComponent<InputField>().text != "" &&
Input.GetKeyDown(KeyCode.Return))
{
LoginButton();
}
}
public void LoginButton()
{
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Roll A Ball;Integrated Security=True");
bool login = false;
con.Open();
string sql = "SELECT * FROM Login WHERE Username='" + Username + "' and Password='" + Password + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
login = true;
}
dr.Close();
con.Close();
if (login == true)
{
Application.LoadLevel(1);
}
else
{
errormessage.text = "Failed To Log In";
}
}
}
Screenshots from the MSSQL DB table:
i hope i was clear enough and if u want more codes or even my project. write a comment. thank you in advance.
EDIT:
i think i wasn’t clear enough. i have 2 databases: Login and Highscore. i also have a Login page. in the login page when i try to login, i use a Username and a Password. the Username is saved in the script Login. but after the Login i need to know with which username i logged in so i want to save the Username which i used in the Login page in another script so i can use that Username to save my highscore.
In General: is there a way how to acces or save a Variable in another script?