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 Xeong-Hu · Mar 01, 2014 at 01:25 PM · positioninggui-button

How can i make my GUI Button appear above my clicked object?

Alright here's what i'm doing.

I'm trying to make a 3D Card game.

So when ever I click a card that is in my hand, I want a GUI.Button to appear over my card Object because i clicked it. And the button appear as Summon so when i click summon it'll apply it's effect. Pretty simple right? Can any1 help please?

BTw. Here's my existing code. for FUnction GUI.

 function OnGUI()
 {        var x = Event.current.mousePosition.x;
         var y = Event.current.mousePosition.y;
     if (clicked == true)
     {
        
       if (GUI.Button (Rect (x,y, 100, 20), "Summon")) {
         
         ApplyEffect = true;
         clicked = false;
         
 
     }
     else {
     }
        }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by robertbu · Mar 01, 2014 at 07:29 PM

Getting something like this working is often fussy. It is not just about placing the button to go away if the user clicks outside the cards, and you want the button to move if the user clicks on another card, and the button has to go away if the user uses the button. Here is a bit of code to get you started. It should be placed on a single object in the scene. It assumes that your cards are tagged 'Card'. I assume that you will want different behaviors based on what card is used in the summoning. If so, you can get the information from the 'hit'. You can use either the name of the object (hit.collider.name), or you can use hit.collider.GetComponent() to get the script with specific info or to execute methods on that card.

 #pragma strict
 
 private var showButton = false;
 private var pos : Vector2;
 private var hit : RaycastHit;
 
 function OnGUI() {
     var e = Event.current;
 
     var x = pos.x - 50;  // Calc x and y to center of button
     var y = pos.y - 10;
 
     if (showButton && GUI.Button (Rect (x,y, 100, 20), "Summon")) {
         Debug.Log("Summoning");
         showButton = false;
     } else {
         if (e.type == EventType.MouseDown) {
             CheckClick();
         }
     }
 }
 
 function CheckClick() {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
     if (Physics.Raycast(ray, hit)) {
         if (hit.collider.tag == "Card") {
             pos = Camera.main.WorldToScreenPoint(hit.transform.position);
             pos.y = Screen.height - pos.y;  // convert from Screen to GUI
             showButton = true;
         }
     }
     else {
         showButton = false;
     }
 }

   
Comment
Add comment · Show 3 · 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 Xeong-Hu · Mar 02, 2014 at 07:45 AM 0
Share

Alright cool Thanks a lot man. It took longer than i'd expect for a response. Guess it wasn't an easy question.

But i have an ssue sadly..

When ever i click my card, the Button doesn't appear.

Here's what i did.

I made an empty Object named Click Effect Summon. And dragged it up so it'll be out of the way.

Then I put a new Script in it called Summon Click. The Script you gave me.(This is the only Script in there)

I Disabled my old Script. (But for Some reason when i click my card it still checks the Click Check box.) <-- I don't think that's much of an issue.

And Last but not least my card game Object is Tagged as Card.

Idk what i did wrong.

Can You help me out please? Thanks for giving me a code though, atleast now i have a visual of what i'm working with.

avatar image Xeong-Hu · Mar 02, 2014 at 08:28 AM 0
Share

I also looked up the function CheckClick on the Unity script Documentation and found out Check Click function actually doesn't exist

avatar image Xeong-Hu · Mar 02, 2014 at 08:46 AM 0
Share

Wait a second. Now it's Working! Idk what the hell happened? But i didn't change anything. all i did was reset my Unity SScript Explorer and it wants to work.

Lol. Anyways Thanks dude. Can't believe we have to type all that just to get a script like this functional. lol thanks.

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

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to control keyboard via GUI button. 1 Answer

Making a Hover function for a button or toolbar from code. 2 Answers

controlling rigidbody 2d 0 Answers

Detect Touch on GUI.Button in Script? 1 Answer


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