- Home /
If Statement within OnGUI not working - any ideas?
I really feel like the below code should work. The Debug test is working fine but I can't get the boxed texture to appear onscreen.
This should be simple but I just can't see why it's not working. Much appreciate the help.
var texture: Texture2D;
function OnGUI () {
if(Input.GetMouseButtonDown(0))
{
GUI.DrawTexture(Rect(100,50,200,200),texture,ScaleMode.ScaleToFit, true, 10.0f);
Debug.Log("YES");
}
}
I wish! Tried that already, moved it around to multiple locations and resized. Before adding the if statement I tested simply GUI.Label and it worked fine.
Any other ideas...I appreciated the help.
Get$$anonymous$$ouseButtonDown() only calls one frame, use Get$$anonymous$$ouseButton() ins$$anonymous$$d.
That worked! Thanks. I voted for your response and am going to give the detailed answer to sparkzbaraca.
Answer by sparkzbarca · Nov 24, 2012 at 07:20 PM
the mouse button may GO DOWN AND GO UP all without ever having called OnGUI.
That code above wont work either its still the same problem He knew the problem just mixed up the solution
update is called as many times as possible. OnGui is called a fixed amount of times per second.
if update is called 4 times a second and OnGui is called 1 time a second
mouse is up
update registers at .25 seconds getmousebutton(0) = false;
OnGui runs mouse isn't down so it does nothing
mouse goes down
update registers at .50 seconds get mouse = true;
mouse goes up
update registers at .75 seconds still get mouse = false;
mouse still up
update rigsters at 1 second still false
ONGui is ran polls the get mouse its false;
We can see here that the mouse click happened faster than OnGui could register
if you click and release in 1 second for example and OnGui runs every 10 seconds you have 9 seconds or so inside there where you could click and OnGui wont register it.
what you need is
bool mousedown = false;
if(Input.GetMouseButton(0))
mousedown = true;
ongui(){
if(mousedown)
{
//do something
mousedown = false;
}
}
That will work because when the mouse goes down you store it basically. You say the mouse is down and you remember it went down until the next time it polls for the mouse. Then you allow it to reset.
mark as answered and have a nice day.
Answer by DeveshPandey · Nov 24, 2012 at 06:52 PM
Try this.
var texture: Texture2D;
function OnGUI () {
if(Input.GetMouseButton(0))
{
GUI.DrawTexture(Rect(100,50,200,200),texture,ScaleMode.ScaleToFit, true, 10.0f);
Debug.Log("YES");
}
}
Answer by VincentRodriguez · Nov 24, 2012 at 06:52 PM
I think you wrote "var texture: Texture2D", this " : " written with the variable, split them as here "var texture : Texture2D"
Your answer

Follow this Question
Related Questions
GUI.skin not working inside if 2 Answers
Encaplsue function in if statement 1 Answer
Canvas vs OnGUI 1 Answer
GUI.Window only shows for one frame under all circumstances 2 Answers