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