- Home /
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 {
}
}
}
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;
}
}
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.
I also looked up the function CheckClick on the Unity script Documentation and found out Check Click function actually doesn't exist
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.