How to change scenes when pressing sprite

I have a sprite of a button that shows up when you die and I want to make it so when you press it that it reloads the level. (Application.LoadLevel()) How do I get it to change the scene when I click on the button but nothing else.

NOTE: This is NOT using GUI.button or any GUI elements, it is simply a sprite/gameobject.

1 Answer

1

You want this Unity - Scripting API: MonoBehaviour.OnMouseDown()

You need a collider on the sprite so that unity knows it is there and it is intractable.

using UnityEngine;
using System.Collections;


public class LoadOnTouchOrMouse: MonoBehaviour {
    void OnMouseDown() {
        Application.LoadLevel("SomeLevel");
    }

}

This will work on android, if you want a more robust solution you can raycast touches yourself to emulate a mouse. That way you have a but more control in multi-touch scenarios.

Thanks! Cant believe I didn't think of that.. I was trying to use raycasting but I couldn't figure that out either, so this suits me fine!

Just remember is you want to start moving around that sprite you will want to put a kinematic rigidbody on it, otherwise you are moving a static collider and that kills performance.