- Home /
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.
Answer by JanusMirith · Oct 15, 2014 at 01:00 AM
You want this http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
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.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
touch position to world position 2D 2 Answers
How to make a menu like ironpants 0 Answers
Reloading the scene causes ads to reload. Is this ok ? 3 Answers
Android Button Text 1 Answer