Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Arsaceus · Aug 22, 2015 at 10:48 PM · javascriptguiraycastbutton

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: alt text

alt text

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


rsz-21.png (327.7 kB)
rsz-12.png (269.8 kB)
Comment
Add comment · Show 13
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 Arsaceus · Aug 24, 2015 at 01:58 PM 0
Share

Anyone? please

avatar image fafase · Aug 25, 2015 at 08:38 AM 0
Share

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.

avatar image fafase · Aug 25, 2015 at 08:52 AM 1
Share

At the top of this page, Unity-Services-Showcase-Learn

Click learn. Hours of fun (and headache) ahead of you.

avatar image Positive7 · Aug 25, 2015 at 09:45 AM 1
Share

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
        }
avatar image Positive7 · Aug 25, 2015 at 10:35 AM 1
Share

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 : ");
       }
   }
 }
Show more comments

2 Replies

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

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

Comment
Add comment · Show 1 · 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 Arsaceus · Aug 26, 2015 at 08:28 PM 0
Share

Thank you very much, with a little modification I got what I was looking for!

avatar image
0

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.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[Logical Error] Select a game object and perform actions using GUI.Button 1 Answer

How to activate a button? 1 Answer

Button OnClick Applied To Whole Canvas? 1 Answer

GUI.Toggle to only send one action, not repeated 1 Answer

Scripting a GUI button to move object smoothly 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