Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dunipan · Nov 24, 2012 at 06:19 PM · onguiif

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");
         }
         }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image greatestprez · Nov 24, 2012 at 06:29 PM 0
Share

maybe your texture is being displayed offscreen?

avatar image dunipan · Nov 24, 2012 at 06:37 PM 0
Share

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.

avatar image TheDarkVoid · Nov 24, 2012 at 06:42 PM 1
Share

Get$$anonymous$$ouseButtonDown() only calls one frame, use Get$$anonymous$$ouseButton() ins$$anonymous$$d.

avatar image dunipan · Nov 24, 2012 at 07:42 PM 0
Share

That worked! Thanks. I voted for your response and am going to give the detailed answer to sparkzbaraca.

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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");
  }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by XienDev · Nov 24, 2012 at 07:09 PM

Are u sure your texture is not empty ?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Can't draw GUI.Label text on subpixel values 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges