Hello there! I need to wait until user click button and set some value. This value is needed in another class as shown below. I have no idea how to implement. I’ve tried to use coroutines but no matter what I do Method1 always returns value before it is set by user. I’ve also tried do…while but obviously it freezes program.
class Class1{
if(Class2.Method1())
//do something
else
//do something else
}
- class Class2{
bool Method1() {
ShowDialog();
WaitUntilUserSetValue();
CloseDialog();
return value;
}
}
Any help & tips are welcome!
Thank you for your further explanation in the comments below. I edited this answer depending on your information. Try it, i hope it will work, i haven’t tested it.
public class Class1 : MonoBehaviour {
public GameObject confirmationDialog; // To set in inspector
public GameObject gameObjectToDelete; // To set in inspector
private bool clickedYes = false;
private bool clickedNo = false;
// Function is called if the object has du be deleted
public void onDeleteClick()
{
StartCoroutine(deleteGameObject(gameObjectToDelete));
}
IEnumerator deleteGameObject(GameObject obj)
{
clickedNo = false;
clickedYes = false;
confirmationDialog.SetActive(true);
while (clickedYes == false && clickedNo == false)
{
yield return 0;
}
confirmationDialog.SetActive(false);
Destroy(gameObjectToDelete); // ... or delete file ... or anything else
}
// function is called when button yes is clicked in the confirmation Dialog (Event)
public void confirmationDialogClickedYes()
{
clickedYes = true;
}
// function is called when button no is clicked in the confirmation Dialog (Event)
public void confirmationDialogClickedNo()
{
clickedNo = true;
}
}
Thanks to you I finally found working solution. Here is pseudocode. Maybe someday will be helpful for someone.
class Class1 {
void onButtonClicked(){
Class2.Method1(Action SomethingToDo);
}
void SomethingToDo(){
...
}
}
-
class Class2 {
Action actionToDo;
bool userClicked = false;
bool confirmed;
void Method1(action){
actionToDo = action;
ShowDialog();
StartCoroutine(WaitForUserConfirm());
}
IEnumerator WaitForUserConfirm(){
yield return new WaitUntil(() => userClicked);
if(confirmed)
actionToDo();
CloseDialog();
}
void onConfirmButtonClicked(){
userClicked = true;
confirmed = true;
}
void onDeclineButtonClicked(){
userClicked = true;
confirmed = false;
}
}
Have a look at this youtube channel:
Dialog System
Channel