- Home /
[Logical Error] Select a game object and perform actions using GUI.Button
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? should I use another way of selecting objects instead of ray cast or else?
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611235300im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Select a game object and perform actions using GUI.Button (EventTrigger) 2 Answers
Can't get 4.6 GUI Button to Load Scene (Solved) 1 Answer
GUI Button Disappearing 1 Answer
I have doubt please help me 2 Answers
Unity buttons touchscreen woes 0 Answers