- Home /
Access Array From Another Script?
Hey guys, so I'm trying to use an Array to circumvent creating an actual server for login information since the program I'm making if for home use, I see no point in a PHP. Anyway so far what I have is this.
First Script:
//Strings
var firstName : String = String.Empty;
var lastName : String = String.Empty;
var desiredUser : String = String.Empty;
var desiredPass : String = String.Empty;
var confirmPass : String = String.Empty;
//Arrays
var registeredUsers : Array = [];
//Booleans
var passwordIsSame : boolean = false;
var userNameChosen : boolean = false;
var displayMessage : boolean = false;
//floats
var displayTime : float = 3;
function Start () {
}
function Update ()
{
if (displayMessage)
displayTime -= Time.deltaTime;
if(displayTime <= 0.0)
displayMessage = false;
if(desiredUser.length > 0 && desiredUser.length <= 15)
userNameChosen = true;
}
function OnGUI ()
{
//If Statements
if(displayMessage)
{
GUI.Label(new Rect (Screen.width/ 2 - 110, Screen.height/ 2 + 145, 500, 500), "Some information has not properly been filled in.");
}
//Labels
GUI.Label(new Rect(Screen.width/ 2 - 57, Screen.height/ 2 - 23, 200, 200), "Please Register an Account.");
GUI.Label(new Rect(Screen.width/ 2 - 120, Screen.height/ 2, 200, 200), "First Name:");
GUI.Label(new Rect(Screen.width/ 2 - 119, Screen.height/ 2 + 23, 200, 200), "Last Name:");
GUI.Label(new Rect(Screen.width/ 2 - 164, Screen.height/ 2 + 46, 200, 200), "Desired Username:");
GUI.Label(new Rect(Screen.width/ 2 - 161, Screen.height/ 2 + 69, 200, 200), "Desired Password:");
GUI.Label(new Rect(Screen.width/ 2 + 103, Screen.height/ 2 + 46, 200, 200), "(Limit of 15 characters.)");
GUI.Label(new Rect(Screen.width/ 2 + 103, Screen.height/ 2 + 69, 200, 200), "(Limit of 25 characters.)");
GUI.Label(new Rect(Screen.width/ 2 - 162, Screen.height/ 2 + 92, 200, 200), "Confirm Password:");
//TextFields
firstName = GUI.TextField(Rect(Screen.width/ 2 - 50, Screen.height/ 2, 150, 20), firstName, 99);
lastName = GUI.TextField(Rect(Screen.width/ 2 - 50, Screen.height/ 2 + 23, 150, 20), lastName, 99);
desiredUser = GUI.TextField(Rect(Screen.width/ 2 - 50, Screen.height/ 2 + 46, 150, 20), desiredUser, 15);
desiredPass = GUI.PasswordField(Rect(Screen.width/ 2 - 50, Screen.height/ 2 + 69, 150, 20), desiredPass, "*"[0], 25);
confirmPass = GUI.PasswordField(Rect(Screen.width/ 2 - 50, Screen.height/ 2 + 92, 150, 20), confirmPass, "*"[0], 25);
//GUI Buttons
if(GUI.Button(Rect(Screen.width/ 2 - 28, Screen.height/ 2 + 115, 100, 30), "Register"))
{
if(desiredPass.length > 0)
{
if(desiredPass == confirmPass)
{
passwordIsSame = true;
}
}
if(passwordIsSame && userNameChosen)
{
registeredUsers.Push (desiredUser);
for (var value : String in registeredUsers)
{
print(value);
}
Application.LoadLevel("Login");
}
if(!passwordIsSame || !userNameChosen)
{
displayMessage = true;
displayTime = 3.0;
}
}
}
That is used in a different scene which is for registering an account. It works too. The username you choose is pushed into the array. The thing is I want to access it in the other scene at the Login menu so it can check if the user matches what is stored in the array.
Here's the second script:
var userToEdit : String = String.Empty;
var passToEdit : String = String.Empty;
var gameObjectArray : GameObject[] = GameObject.FindWithTag("arrayObject").GetComponent("RegistrationScript").registeredUsers;
function Start () {
}
function Update ()
{
}
function OnGUI ()
{
//Aestetics
GUI.Label(new Rect(Screen.width / 2 - 117, Screen.height / 2 + 13, 150, 20), "Password:");
GUI.Label(new Rect(Screen.width / 2 - 120, Screen.height / 2 - 10, 150, 20), "Username:");
GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 40, 500, 500), "Please Enter Your Username and Password.");
userToEdit = GUI.TextField(Rect(Screen.width / 2 - 50, Screen.height / 2 -10, 150, 20), userToEdit, 25);
passToEdit = GUI.PasswordField(Rect(Screen.width / 2 - 50, Screen.height / 2 + 13, 150, 20), passToEdit, "*"[0], 25);
//Buttons and Functionallity
if(GUI.Button(Rect(Screen.width / 2 + 30, Screen.height / 2 + 35, 100, 30), "Register"))
{
Application.LoadLevel("Registration");
}
if(GUI.Button(Rect(Screen.width / 2 -80, Screen.height / 2 + 35, 100, 30), "Login"))
{
}
}
What I want to do is at the bottom where the final button is i want to create an if statement that will ask if(userToEdit == 'any username stored in the database') Debug.Log("This works.")
Or something like that for confirmation so I can focus on optimizing it next.
Any help would be greatly appreciated.
Answer by 34638a · Aug 11, 2014 at 11:27 AM
to read the array most effectively, what you should do is create a script with a class inside it that holds the data for the array, then use that class as a reference for an array:
var MyArray : ArrayClass[];
//separate script named ArrayClass
class ArrayClass { //stuff you want to have for the array }
what this will do is let you use that same class for multiple arrays so you can have the data directly transfer: ThisScript.MyArray1[0] = ThatScript.MyArray2[0];
When trying to create a variable with the type ArrayClass it states "The name 'ArrayClass' does not denote a valid type ('not found').
Answer by GimLee · Aug 17, 2014 at 02:57 AM
First, make sure the array is public. What I do is always this.
private PlayerController player;
void Start () {
player = GameObject.Find("PlayerObjectName").GetComponent<PlayerController>();
}
Then I can always use the public variables/functions by eg. player.speed = 0;
Sorry for the C#. Also, I don't know if it will work when the scripts are from two different scenes.
All that really needs to be done is for the Username the person inputs to be saved to an array. That way on the login screen when you press "Login" it will search the Array for a match and say whether or not to Login.
Your answer
