Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm creating a 2D project which does not have a character. So i created an empty gameobject and attached the script to it. In the script, i have declared other objects which are in the scene like this:

public GameObject obj1,obj2,obj3;

How can I know if obj1 or obj2 or obj3 was clicked? Is raycasting the only solution?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Yes, you can use raycasting to detect the objects in your scene. You don't need to attach a custom script to the game objects you want to detect, but you do need to attach colliders to them.

In the update method of your script, attached to the otherwise empty object, you can check for when the mouse button is pressed. Then, cast a ray into the scene from the camera, through the mouse. Something like the following:

void Update() {
    if (Input.GetMouseButtonDown(0)) {
        Debug.Log("Pressed left click, casting ray.");
        CastRay();
    }
}

void CastRay() {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, 100)) {
        Debug.DrawLine(ray.origin, hit.point);
        Debug.Log("Hit object: " + hit.collider.gameobject.name);
    }
}
share|improve this answer
    
Thanks. The first Debug line is printed to console. Then i get this: UnityEngine.Debug:Log(Object). But the last Debug line is not shown. –  SanSolo Sep 4 at 14:33
    
Also, the raycast is firing when i click my GUI buttons too. Is that a problem? –  SanSolo Sep 4 at 14:39
    
Read the documentation on raycasting. You need to add colliders to the objects you want to be detected with the ray. Yes, the ray will be cast any time you press the mouse button, that includes when pressing for GUI items, you'll need to detect separately when clicking a GUI item and not allow the raycast in those situations. That's an entirely different question though and not suitable to be addressed in the comments of this answer. –  Byte56 Sep 4 at 14:50
    
Yes I added BoxCollider2D to the object. Object name is not shown in the console. –  SanSolo Sep 4 at 14:55
    
If it's a 2D collider, you need the 2D raycast. –  Byte56 Sep 4 at 15:23

I modified the script and now it works. First, i removed the

public GameObject obj1,obj2,obj3; //*Although this shouldn't affect the script.

The updated script is as follows:

using UnityEngine; using System.Collections;

public class ClickTest : MonoBehaviour {
    //public GameObject red,red2,yellow,yellow2,MainCamera;
    // Use this for initialization

    void Start () {
        }
        // Update is called once per frame
    void Update () {
            if (Input.GetMouseButtonDown(0)) {
            Debug.Log("Pressed left click, casting ray.");
            CastRay();
        }

    }

    void CastRay() {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
        if (hit) {
                        Debug.Log (hit.collider.gameObject.name);
                    }
            }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.