Loading Scene on MouseClick

I am trying to make a GameObject operate as a button by detecting a mouse click on it. So far, I am using this

using UnityEngine;
using System.Collections;

public class MenuPlay : MonoBehaviour
{
    void OnMouseDown()
    {
        if (Input.GetMouseButtonDown(0)) {
            Application.LoadLevel ("InsideBuilding");
        }
    }
}

This obviously doesn’t work, so why doesn’t it? Is the syntax not proper for GetMouseButtonDown or is it something else? Yes, the script is attached to the Play1 object. Any help appreciated. Thanks!

… then why “find” the Play1 object? and for that matter why cast the GameObject to an Object?
also… why have that at all? it doesn’t appear to do anything in your code? :eyes:

per the scripting reference: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
OnMouseDown requires a collider (ignore GUIElement, that’s legacy ongui stuff :slight_smile: ), does your gameobject have one attached?

as an aside, Application.LoadLevel is obsolete has been replaced by SceneManager.LoadScene. It’ll still work for the time being but it might be worth learning the new stuff since it’s taking over :slight_smile:

Ill clean it up a bit, but the object has a mesh collider on it. Would something else be better?

put this

using UnityEngine;
using System.Collections;

public class Testing : MonoBehaviour
{
    void OnMouseDown()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("left click");
        }
    }
}

on a cube with it’s default collider swapped for a mesh collider (of cube mesh)… works fine.

when you say “obviously doesn’t work”, any errors?

is there anything that would block a raycast to the gameobject you’re clicking on?

Simply adding a box collider worked just fine. The mesh collider came from importing the model I am using as an FBX, so it wasn’t specific enough. The box works just fine. Thanks