- Home /
Select a game object and perform actions using GUI.Button (EventTrigger)
I am currently working on a strategy game and I want to preform actions using GUI.Button on game objects. I am using ray cast and mouse click to select the object however when I click on GUI.Button to take out another action the button disappears. I want to use that button to open up another GUI.Box to show some descriptions.
I know why the button is disappearing, it is because I am projecting the ray cast to my button clicks in the update function but how can I avoid this? I also know that I have to use EventTrigger however I am not familiar with javascript event trigger, I searched online but I couldn't find any helpful javascript.
Screenshots:
Here is my script:
pragma strict
@HideInInspector
var isCalled:int = 0;
@HideInInspector
var scWidth:int = 0;
@HideInInspector
var scHeight:int = 0;
function Start () {
scWidth = Screen.width;
scHeight = Screen.height;
}
function Update () {
if (Input.GetMouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if (hit.collider.tag == "House") {
isCalled = 1;
} else{
isCalled = 0;
}
}
}
}
function OnGUI(){
if(isCalled==1)
GUI.Button(Rect(scWidth/2,(scHeight/2)+(scHeight/4),120,120), name);
}
Why using the old GUI? Unity spent months and thousands to get a proper UI system running and you flip your finger at them using the old one. $$anonymous$$ore seriously, it is easier to achieve with the new system using events.
At the top of this page, Unity-Services-Showcase-Learn
Click learn. Hours of fun (and headache) ahead of you.
Hmm... Interesting It works fine here. The Raycast checks if you clicked the house with mouse pointer and shows the Button then you need another if Button is clicked then Do Something:
if(GUI.Button(Rect(scWidth/2,(scHeight/2)+(scHeight/4),120,120), name)){
//Do Something
}
This will show Button above the object (if I'm not mistaking) :
var scWidth:int = 0;
@HideInInspector
var scHeight:int = 0;
var showInfo:boolean;
var showBox:boolean;
private var pos : Vector2;
function Start () {
scWidth = Screen.width;
scHeight = Screen.height;
}
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if (hit.collider.tag == "House") {
showInfo = !showInfo;
pos = Camera.main.WorldToScreenPoint(hit.collider.transform.position);
pos.y = Screen.height - pos.y;
}
}
}
}
function OnGUI(){
var x = pos.x - 60; //center of GUIButton = half of the GUIButton width
var y = pos.y - 100;
if(showInfo){
if(GUI.Button(Rect(x,y, 120, 60),name)){
showBox = !showBox;
}
if(showBox){
GUI.Box(Rect(x + 125, y, 120, 120),"HouseInfo : ");
}
}
}
Answer by thomassquall · Aug 26, 2015 at 12:29 PM
I didn't get what you want to do but anyway what I see from your script is that when you hit an object with the tag 'House' you show the Button, otherwise you don't.
First I suggest you to change var isCalled:int, to var isCalled:boolean so you can use true and false values then, hopefully this is what your looking for, you should change the code into this:
@HideInInspector
var isCalled:boolean = false;
@HideInInspector
var isButtonPressed:boolean = false;
@HideInInspector
var scWidth:int = 0;
@HideInInspector
var scHeight:int = 0;
function Start () {
scWidth = Screen.width;
scHeight = Screen.height;
}
function Update () {
if (Input.GetMouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if (hit.collider.tag == "House") {
isCalled = true;
} else{
isCalled = false;
}
}
}
}
function OnGUI(){
if (isCalled || isButtonPressed)
if (GUI.Button(Rect(scWidth/2,(scHeight/2)+(scHeight/4),120,120), name))
{
isButtonPressed = true;
// do whatever you want
}
}
Anyway once you press the button you'll be not able to hide it anymore unless you will add something that reset the isButtonPressed variable, but this is up to you because I don't know the logic you want to use to hide it.
Hope this helped
Thank you very much, with a little modification I got what I was looking for!
Answer by Arsaceus · Aug 26, 2015 at 07:38 AM
The else statement in update function is the problem, after I removed it my problem has been solved.