- Home /
trouble scripting a password list to access scene,Script to check UI Input Field against a list of passwords
I'm trying to create a script that compares a public list of passwords with the inputField text to see if the text matches one of 100 possible pre-defined passwords.
I'm really new to scripting. I've come a long way though.
I've created a working public list with a script public List passwords = new List();
I'm able to create a button action that gets me to the next scene SceneManager.LoadScene("Scene2_Hearts", LoadSceneMode.Single);
But for the life of me i can't figure out the IF statement that looks into my List of passwords and decides if the input field text matches one of the options in order to allow the button to trigger.
Anyone out there able to help? Thanks y'all. ,I'm trying to create a c# script where I have a public list of 100 predefined passwords. I'd like it so a person has to type a password into an input field, then click the "submit" button, and have that password match one of the ones in my list in order to access the next scene.
I've got down the public list: public List passwords = new List();
I've figured out the button calling up a scripted function to get to the next scene SceneManager.LoadScene("scene2", LoadSceneMode.Single);
but for the life of me i can't figure out an IF statement that sees if the GUI text field for inputfield matches one of the list items in my passwords script. It's driving me bonkers.
Anyone know a solve?
Thanks y'all.
Answer by smkmth · Nov 26, 2017 at 12:21 AM
the way i would do it is a public reference to the InputField, and then make a public method called somthing like GetPassword() which takes a string value taken from InputField.text - and then checks if that value is in the password list with .Contains(), then set the method to go when you click the submit button, by dragging the obejct which has the script to the OnClick() section in the passwords inspector so like public InputField passwordField;
public void GetPassword(){ string input = passwordField.text; if(passwords.Contains(input )){ SceneManager.LoadScene("scene2", LoadSceneMode.Single) } }
from there you can either have a lot of if statments - which say like - if (input == "specficpasswordA"){ SceneManager.LoadScene("SpecificLevelA", LoadSceneMode.Single);} if (input == "specificpasswordB"){ scneMananger.LoadScene("specficLevelB", LoadSceneMode.Single); } or just pass the input to the scenemanager directly, and name the scene the password you want to associtae with it. lets say you want the password for scene2 to be "Scene2Password". Change the name of scene 2, then in the method write SceneManager.LoadScene(input, LoadSceneMode.Single); - and it will try to load whatver scene you pass in to input. obviously you might need a check to see if that scene exists - it might just be easier to do it with the if statements, but whatever works man.
i am also new , but i feel this will work.
Your answer
Follow this Question
Related Questions
Public list are not reflecting in inspector. 1 Answer
My list is overwriting the data it was holding with the newest element added to the list. 2 Answers
Why is my list not filling 1 Answer
Access list storing custom class variables from another script 1 Answer
Temporary GameObject Movement 0 Answers