I know this is kind of a common issue, and I’ve looked at many other threads about this. I’ve gone over the checklist of potential problems:
- Made sure the script is attached to the game object I want to be clickable
- Made sure I have an enabled collider
- Made sure I have not resized or changed the position of the collider
- Made sure there are no other colliders in the way.
But still, nothing happens when I click on my object. Below is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GeoObject : MonoBehaviour {
public Sprite[] shapes;
public float delay = 2.0f;
public bool clicked = false;
public float switchTime;
public int spriteIndex = 0;
// Use this for initialization
void Start () {
var renderer = GetComponent<SpriteRenderer>();
// Randomizes the starting shape
System.Random rand = new System.Random();
spriteIndex = rand.Next(0, shapes.Length - 1);
renderer.sprite = shapes[spriteIndex];
}
void OnMouseDown()
{
Debug.Log("You have clicked the object!");
}
}
Additionally, as per the advice from another thread, I also attached the following script to an empty GameObject, to get an idea of what was happening when I tried to click on an object:
public class RayCastExample : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log("Name = " + hit.collider.name);
Debug.Log("Tag = " + hit.collider.tag);
Debug.Log("Hit Point = " + hit.point);
Debug.Log("Object position = " + hit.collider.gameObject.transform.position);
Debug.Log("--------------");
}
}
}
}
Still, nothing seems to happen when I click. I would really appreciate any help with this.
Thanks in advance!