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:
52485-rsz-21.png

52486-rsz-12.png

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);
 }

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

The else statement in update function is the problem, after I removed it my problem has been
solved.