Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LinkUpGames · Aug 11, 2014 at 09:30 AM · array

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

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];

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LinkUpGames · Aug 17, 2014 at 02:54 AM 0
Share

When trying to create a variable with the type ArrayClass it states "The name 'ArrayClass' does not denote a valid type ('not found').

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LinkUpGames · Aug 17, 2014 at 03:11 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I Resize an Array in JS? 1 Answer

Using a list of arrays to make Unity UI 2D button toggle colors 0 Answers

Will an array of Textures affect the performance? 1 Answer

Using Vector3[] to store vector3 of objects, 1 Answer

accessing to one array from another script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges