Question by
Psycholatte · Aug 01, 2018 at 10:03 PM ·
c#uibuttonunity5
Second button works 4 times
I wrote the following code. There is a button that says Report, and two more buttons appear when you click it. The first one of them works just fine, but when I use the second one I get 4 Debug statements. What could be the problem?
public class OpenWindow : MonoBehaviour
{
public Rect windowRect;
bool showWindow = false;
public Button b1;
public LocData loc;
public GameObject inp;
public GameObject inp1;
public InputField field;
public InputField field1;
private bool vel = false;
public void onbutton()
{
showWindow = true;
}
void OpWindow()
{
if (showWindow == true)
{
windowRect = GUI.Window(0, new Rect(b1.transform.position.x-50, Screen.height*0.31f , 120, 80), DoMyWindow, "Report");
}
}
void OnGUI()
{
{ OpWindow();}
}
// Make the contents of the window
void DoMyWindow(int windowID)
{
if (GUI.Button(new Rect(10, 20, 100, 20), "Position Report"))
{
field = inp.GetComponent<InputField>();
field1 = inp1.GetComponent<InputField>();
inp.SetActive(true);
inp1.SetActive(true);
}
else if (GUI.Button(new Rect(10, 50, 100, 20), "Velocity Report"))
{
vel = true;
field = inp.GetComponent<InputField>();
field1 = inp1.GetComponent<InputField>();
inp.SetActive(true);
inp1.SetActive(true);
}
}
private void Update()
{
if (field.text != "" && field1.text != "" && Input.GetKey(KeyCode.Return))
{
loc.GetLocReport(field.text, field1.text,vel);
showWindow = false;
inp.SetActive(false);
inp1.SetActive(false);
}
}
}
Comment
Well it wouldn't hurt to say what the debug statements said...
Given that's the issue.
For now I put Debug.Log("You've clicked on Velocity Report) statement and it works 4 times
Answer by Psycholatte · Aug 06, 2018 at 11:17 AM
Okay I fixed it. Simply I changed "GetKey" to "GetKeyDown" and it worked just fine.