- Home /
Question was not the problem with the code
How to begin startcoroutine (IEnumerator) with UI button.
I've been trying to find an answer (that I can understand) for this for a couple of hours but I haven't been able to find anything.
I'm a very new beginner and I'm following this tutorial: https://www.youtube.com/watch?v=-GWwfBqmWaA∈dex=7&list=PLTm4FjoXO7ndjPE8JXrJ9MjMfsJv956Qm
Only, I would like to get get the username and password from inputfields instead of public strings and I'd like to submit by clicking a UI button instead of using "if(Input.GetKeyDown(KeyCode.L))"
Only I haven't been able to.
What I've tried:
- using button.onclick inside the Update() method... but I can't for the life of me figure out how to use that function.
- creating a separate method to try to start the co routine from the UI button onClick system.
I'm not completely sure how a coroutine and IEnumerator works but I know that I need it to be able to wait for the www variable to finish.
Here's the code I'm currently not able to get working.
public InputField inputUsername;
public InputField inputPassword;
string LoginURL = "http://localhost/languinator/login.php";
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//if (Input.GetKeyDown(KeyCode.L)) StartCoroutine(LoginToDB(inputUsername, inputPassword));
}
IEnumerator LoginToDB(string username, string password)
{
WWWForm form = new WWWForm();
form.AddField("usernamePOST", username);
form.AddField("passwordPOST", password);
WWW www = new WWW(LoginURL, form);
yield return www;
Debug.Log(www.text);
}
void LoginMethod()
{
LoginToDB(inputUsername.text, inputPassword.text);
}
Answer by OGDE01 · Jan 05, 2019 at 03:36 AM
What you should be able to do is create a new public void. In the void, you can call your coroutine. Then, in the inspector, select your button and expand the "Button" component. If you scroll to the bottom, the OnClick method for the button is run here. Click + and drag the object with this script into the Object field. Then, click the button **NEXT* * to where it says runtime and select the script, and select the new method you created. If you need me to explain further, please say so.
Hope this helps!
Follow this Question
Related Questions
I can't start a coroutine. I get a weird message from visual studio 1 Answer
i want my star () function to work my button is clicked 0 Answers
Can anyone tell me why script is not working? 2 Answers
Button OnClick() only being called once? 3 Answers
[Solved] Button OnClick properties are missing after loading the scene 4 Answers