Skip to main content

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

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

The updated script is as follows:

using UnityEngine; using System.Collections;

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

}

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

}

I modified the script and now it works. First, I remove:

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);
        }
    }    
}
Source Link
SanSolo
  • 1.2k
  • 1
  • 10
  • 29

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

}