Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Pflobus · Dec 07, 2014 at 11:27 PM · c#gui

Need help with GUI and raycast...and gameobject I click on, I get the GUI to show up.

Like the title says, I need help-whenever I click on ANY gameobject, the GUI shows up. I want it so be so that whenever I click on a specific object, then and for ONLY that object, the GUI shows up. So if I click on any other gameobject or mesh, the GUI won't show up. Here's my script :)

using UnityEngine; using System.Collections;

public class Click : MonoBehaviour {

 public Texture tex;
 //public float GUItrialone = GUI.Button(350, 350, (10, 20 "Try!"))
 bool        displayGUI = false;
 void    OnGUI()
 {
     if (!tex)
         Debug.LogError ("Really Dude? After all this, you forget a TEXTURE???");

     if (Input.GetMouseButtonUp(0))
     {
         Ray            ray = Camera.main.ViewportPointToRay(Camera.main.ScreenToViewportPoint(Input.mousePosition));
         RaycastHit    outinfo;
         
         if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
         {
             //TOUCH! Show GUI
             displayGUI = true;
         }
     }
     if (displayGUI == true)
     {
         if(GUILayout.Button(tex))
             displayGUI = false;
     }


 }

}

Thank you in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ahmedbenlakhdhar · Dec 07, 2014 at 11:33 PM

Try to retrieve the game object from the RaycastHit, like this:

 if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
 {
     if(outinfo.transform.gameObject.name == "your game object"){
         displayGUI = true;
     }
 }
Comment
Add comment · Show 8 · 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 Pflobus · Dec 07, 2014 at 11:46 PM 0
Share

Is there a way to assign a gameobject as a variable, and then call that variable in the "if" logic you have there?

avatar image Pflobus · Dec 07, 2014 at 11:48 PM 0
Share

Because that did work, but I'm going to have like 14 objects, and I don't want to specifically mod a separate script for each one. So is there a way to assign, and then call a variable in the statement you have there?

avatar image static_cast · Dec 07, 2014 at 11:49 PM 0
Share

Yes, but why doesn't this work well enough?

 public GameObject $$anonymous$$YOBJECT = null; //Set this in the editor
 
 if (Physics.Raycast(ray, out outinfo, $$anonymous$$athf.Infinity))
 {
     if(outinfo.transform.gameObject == $$anonymous$$YOBJECT){
         displayGUI = true;
     }
 }
avatar image Pflobus · Dec 08, 2014 at 12:01 AM 0
Share

Well, I'm dealing with a ton of gameobjects, so for me to put this script on everything, and then change it for each object, would turn out to be quite the hassle, and it is BOUND for error, seeing as I am a bit clumsy at my editing :)

avatar image Pflobus · Dec 08, 2014 at 12:01 AM 0
Share

But thanks, to both of you. If there are any errors, I will post them here, possibly as an edit, or answer, or comment....

Show more comments

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

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Separating mouse button clicks from GUI and game input 1 Answer

Custom Editor for C# Dictionary 2 Answers

x0,y0 doesn't always point to the bottom left corner 1 Answer

Timer Help C# - Displaying it in a non Decimal format 1 Answer

GUI font size help 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