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 /
  • Help Room /
avatar image
0
Question by SaurabhStudio · Mar 18, 2016 at 08:51 AM · uigameobjectraycastname

Get Name of UI gameobject with raycast

I wont the name of UI gameobject with help of Raycast.

For example if my finger is on any button with name "Cartoon" then i need that name with help of raycast.

Comment
Add comment · Show 1
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 Soraphis · Mar 18, 2016 at 04:46 PM 0
Share

maybe try GraphicRaycaster.Raycast http://docs.unity3d.com/462/Documentation/ScriptReference/UI.GraphicRaycaster.html

have not used it before, so i'm not sure if it solves your issue

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by meat5000 · Mar 28, 2016 at 07:11 PM

Physics.Raycast doesn't work on a Canvas Graphic but a GraphicRaycaster does.

You can fire one off manually using

 myGRaycaster.Raycast(data, rayResults);

where myGRaycaster is your Graphics Raycaster component on your Canvas, data is PointerEventData and rayResults is a List.<RaycastResult>.

Using the pointer event data supplied the GRaycaster will find all the UI elements touched by the ray and will append them all to the List you supplied.

However, I discovered this can be a needless step if you have included the Pointer Event interfaces in your script.

For example, if you have implemented OnPointerDown or OnDrag etc you can use the PointerEventData populated in that Method to obtain the Raycast information associated with that event without performing an extra raycast.

 #pragma strict
 
 import UnityEngine.UI;
 import UnityEngine.EventSystems;
 
 public class FiringAGraphicsRay extends MonoBehaviour implements IPointerDownHandler
 {
     function OnPointerDown(data : PointerEventData)
     {
         Debug.Log(data.pointerCurrentRaycast.gameObject.name);
     }
 }

The key here is pointerCurrentRaycast member of the PointerEventData which is generated in our interface.

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
avatar image
0

Answer by Zeoli · Mar 18, 2016 at 03:42 PM

you can use raycasthit object to get what was hit. http://docs.unity3d.com/ScriptReference/RaycastHit.html

The example of how to use this is at the bottom of this page. http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

i know this answer is a bit scarce but I do want to show people HOW to find this info just so they know how to resolve another issue next time.

fyi: the "out" keyword basically fills in that object from within the method called. so out hit gives you a plethora of info regarding what was hit. that you can utilise after the method has been called. May be a good idea to put an if statement on it like if(hit != null) just to make sure that you actually hit something otherwise referencing a property of hit would fire an exception.

I hope this helps, if you need further help, just ask. (make sure you use @Zeoli so i get an email that you mentioned me).

Comment
Add comment · Show 2 · 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 SaurabhStudio · Mar 18, 2016 at 04:10 PM 2
Share

hey @Zeoli

I think this will not work on canvas.. I have try this.

I have try 2d also.. maybe I have done some mistake

$$anonymous$$ay be possible you can share answer.

avatar image Zeoli SaurabhStudio · Mar 18, 2016 at 05:30 PM 0
Share

hi @saurabhStudio

just to confirm, this is a touch interface I think you are doing? if so, and you are on a canvas which handles UI, cant you just add the button component to it and put the action you wish to happen in there? you could even get the button to change a bool in the gamemanager to say that it is being pressed. then just run a series of Ifs in the game manager. not the most efficient way but it should do the trick.

I would need more info as to exactly what you are trying to achieve, maybe theres a better way?

avatar image
0

Answer by markncolleen1985 · Dec 27, 2017 at 11:43 PM

Thanks, this is the answer why vr samples does not work with a canvas ui menu, any idea how to make the menu buttons function wiht a collision as a mouse0 would ? Thanks

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

83 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I raycast gameobjects with specific layer ? 1 Answer

Offset between mouse cursor position and "hit" position with worldspace UI's 1 Answer

Unity Editor UI [bug] 2021.2.1f1 silicon 1 Answer

How to physics raycast through overlay UI? 1 Answer

OnMouseDown work through UI elements 3 Answers


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