0
\$\begingroup\$

Message boxes, had some time put in creating some for my program, now it all were informative message boxes where an OK button was available and then it closed.

I worked with a canvas that would be enabled when needed and gets disabled on clicking OK.

But now I'm in need of a messagebox that gives an output. I've tried to do the same and test some stuff with public variables, but my code kept going and didn't wait for the response of the "dialogue box" which results in the changes going through and an unused canvas that opened behind my active screen. (Another canvas is loaded when running the code while the current canvas gets disabled).

Code example:

 cnvCleanup.enabled = true;
 //Here it should wait till i have "answered" on cnvCleanup
 if (CleanUpDone == true)
 {
 usages = 0;
 }
 else
 {
 usages++; 

The cnvCleanup will get disabled on the script I wrote for clicking on the OK button. In case the Not OK button is clicked the bool CleanUpDone should remain false so there the cnv gets disabled.

EDIT: More code. The CleanUpDoneis public bool CleanUpDone; that gets changed in the cnvCleanup.

int usages;  
usages = System.Int32.Parse(AssetList[2]);  
if (usages < 10)  
 {  
 usages++;  
 AssetList[2] = usages.ToString();  
 }  
 else  
 {  
 cnvCleanup.enabled = true;  
 //Here it should wait till i have "answered" on the cnvCleanup  
  if (CleanUpDone == true)  
  {  
  usages = 0;  
  AssetList[2] = usages.ToString();  
  }  
  else  
  {  
  usages++;  
  }  
}
\$\endgroup\$
8
  • \$\begingroup\$ Could you please provide a more complete portion of your code? I have an idea on how to help you but I need more information on how exactly you decided to implement things. \$\endgroup\$
    – Sam R
    Commented Feb 19, 2017 at 1:54
  • \$\begingroup\$ @SamR More code available now :) \$\endgroup\$
    – Budan18
    Commented Feb 24, 2017 at 21:36
  • \$\begingroup\$ Ok so where is this code called? In a function? At Start? In the update? \$\endgroup\$
    – Sam R
    Commented Feb 25, 2017 at 20:05
  • \$\begingroup\$ In the update under the check if I'm ticking on the right button. So you could say it's an onclick function. \$\endgroup\$
    – Budan18
    Commented Feb 27, 2017 at 4:42
  • \$\begingroup\$ Go learn C#, OOP, do algorithms and data structures course, read about SOLID and generics, understand best practices in game dev and Unity, normal patterns, game programming patterns, go through some tutorials on games first while learning stuff above, look at tutorial mistakes and learn actually the logic of code and unity workflow before making game by yourself without tutorial help. It's just an advice, otherwise, you will struggle to find the problems, spend much more time, write not maintainable code and search a long time for what am I doing wrong while getting wrong answers. \$\endgroup\$ Commented Mar 3, 2017 at 14:55

1 Answer 1

0
\$\begingroup\$

I have changed the code like this: in the update he checks if he is on one of the needed buttons. so the mousepointer is on the object and the canvas is active. when one of the buttons is clicked, he does the code, if none of the buttons are clicked, nothing happens.

RectTransform rt = btnCleanupOK.GetComponent<RectTransform>();
    if (Input.mousePosition.x > btnCleanupOK.transform.position.x - rt.rect.width /2 && Input.mousePosition.x < btnCleanupOK.transform.position.x + rt.rect.width / 2 && Input.mousePosition.y > btnCleanupOK.transform.position.y - rt.rect.height / 2 && Input.mousePosition.y < btnCleanupOK.transform.position.y + rt.rect.height / 2 && cnvCleanup.enabled == true)
    {
        isOverOK = true;
    }
    else
    {
        isOverOK = false;
    }

How to check the button is clicked:

 if (Input.GetMouseButtonDown(0) && isOverOK)
    {
        clickedOKButton();
    }

In the clickedOkButton(); is the rest of the code that needed to be run when clicking on OK.

I've done the same for clicking on the later button. I know this isn't optimal, but it works.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .