Search Unity

How to get the name of a object clicked 2D

Discussion in '2D' started by J05HU4, Mar 24, 2018.

  1. J05HU4

    J05HU4

    Joined:
    Nov 7, 2017
    Posts:
    3
    Hello!
    How would I get the name of the object I click on using a ray in 2D. (Not OnMouseOver())

    (Please don't point me to other posts because I have looked at close to all of them but none of them have worked for me, and I want to be able to ask questions specific to my needs.)

    Thank you! :)
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Would OnMouseDown() suffice?
    Code (CSharp):
    1.  
    2. public class AnObject : MonoBehavior {
    3.  
    4.    void OnMouseDown() {
    5.       Debug.Log("You clicked: " + name);
    6.    }
    7. }
    8.  
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    From the RaycastHit2D, you can access the collider/transform, and from there the gameObject.name.
    Is that what you're asking? :)

    Not sure you read every post on the subject.. I imagine that's got to be in some of them :) (not that that matters to me, it's just odd you'd say you read them all.)