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 shads11858 · Sep 05, 2013 at 07:22 AM · guibuttonmouse

Dynamic created button at Mouse Location Moving

I'm trying to create a button when a user clicks on a location on my terrain, it will later have a number of little buttons as images which they can click one and terrain will change depending on the button. But in the process of testing this I've ran into a problem that might be easily solved but as yet I haven't figured it out. I have a method on a in Update() for Input.GetMouseButtonDown(0), this does some other things but in one of the if functions it sets bool variable youSure to true to trigger the OnGUI if function below. The idea was it would display a button as the mouse location for the moment just with yes. But problem is the button moves with the mouse pointer so I can't actually even click it.

Is this because the calling that sets youSure = true after a mouseButtonDown event hasn't finished the if (GUI.Button(new Rect(button.x, button.y, 100, 20), "Yes")){ means that it just keeps running this if statement over and over thus creating new buttons as you move the mouse or should it as I assume it would, once you create the button would stay in that x,y location and not move until clicked and closed?

 void OnGUI(){
 
         if(youSure == true){
             button.x = Input.mousePosition.x;
             button.y = Screen.height - Input.mousePosition.y;
             
             if (GUI.Button(new Rect(button.x, button.y, 100, 20), "Yes")){
                 terrain.PaintTile1x1(tile, 2);
             }
         
         }
     }
Comment
Add comment · Show 2
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 ShadoX · Sep 05, 2013 at 08:54 AM 0
Share

I'm pretty sure that you answer your own question. Try assigning the mousePosition.x/y to new variables when you release the mouse button and then assign those to button.x / y

avatar image shads11858 · Sep 05, 2013 at 09:41 AM 0
Share

I tried a few things to figure out whats going wrong, I even changed the code to something like this and least I get the button come up and stay up, but only down side is when you click the button to activate it's function in the if statement, it get's detected as another mouseclick and opens a new button. Is there anyway to have the button respond only to the click without changing the activation to be say button(2) so left clicking yes won't open a new one?

void Update(){ if(Input.Get$$anonymous$$ouseButtonDown(0)){ button.x = Input.mousePosition.x; button.y = Screen.height - Input.mousePosition.y; youSure = true; } void OnGUI(){ if(youSure){ if (GUI.Button(new Rect(button.x, button.y, 100, 20), "Yes")){ terrain.PaintTile1x1(tile, 2); youSure = false; } } }

0 Replies

· Add your reply
  • Sort: 

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

17 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 avatar image

Related Questions

Right-click button using GUI class? 3 Answers

Menu button needs to be double clicked. 4 Answers

Button being pressed but an other button gets the effect. 0 Answers

Disable a GUI while left mouse button is Down? 2 Answers

How to detect if mouse over a button 2 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