- Home /
OnMouseEnter/Exit not working when pressing button to advance scene
I am writing code for a GUI button, and when both the mouse is over the button (OnMouseEnter/Exit is what I am using) and the mouse is clicked (Input.GetMouseButton(0)) I want to load the next scene. The click part is giving me no trouble, but it is the Enter/Exit part that is not working. I would like to know if I am doing something wrong, or some other way to do both in one (as this is the only way I could find to complete the task). Any help is appreciated!
Code is in C#
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
public class Level1Button : MonoBehaviour {
private bool mouseHover;
void Start()
{
mouseHover = false;
}
void Update ()
{
if (mouseHover && Input.GetMouseButton(0))
SceneManager.LoadScene ("Level 1");
}
void OnMouseEnter()
{
mouseHover = true;
}
void OnMouseExit()
{
mouseHover = false;
}
}
Answer by Kadeo · Jun 09, 2016 at 06:45 PM
I have discovered a very simple way to use a button to activate a scene change. It uses IPointerDownHandler. I will post the code below for anyone else who would like it.
Place the code on the GUI.Button you would like to use
Code is in C#
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class Level1Button : MonoBehaviour, IPointerDownHandler //Make sure that you add this after MonoBehaviour or this WILL NOT work
{
public void OnPointerDown (PointerEventData eventData)
{
if (Input.GetMouseButtonDown (0)) //0 can be changed with 1 or 2, 0 is left click, 1 is right click, 2 is middle click
SceneManager.LoadScene ("Level 1");
}
}
Answer by DizzyWascal · Jun 09, 2016 at 02:58 PM
Is this for the UnityEngine.UI system or GUI.Button rect or is it just a mesh you're applying this script to?
Also there's nothing else really wrong with this script, it should work fine if this is for a 3D text mesh or mesh in particular.
SceneManager.LoadScene("Level 1") should maybe be: SceneManager.LoadScene("Level 1", LoadSceneMode.Single); or that's probably not it because it would default to that anyway.
Hmm did you remember to add the scene you are loading to into the File>Build Settings list?
I am applying this script to a GUI.Button rect, I don't fully understand how those work, and I have tried to find out how to use them, but obviously I am doing something wrong. Is a mesh required for a button to work properly? If so, that would be the problem. There is no mesh on the button or it's text. What kind of mesh would I apply to the button rect?
GUI.button as shown in this documentation You just need to use void OnGUI and you don't need to use On$$anonymous$$ouseEnter and On$$anonymous$$ouseOver.
No you don't need a mesh for a button to work properly, its just that script you showed at the top would actually work on a mesh. Slap it on a cube and click on it because it should work and move you to the specified scene. Also old Unity UI used text meshes so you must of used an old documentation on UI program$$anonymous$$g.
These Button UI are easier to use. You just create a script in it with a:
using UnityEngine;
using UnityEngine.UI; //<-----THIS IS I$$anonymous$$PORTANT
using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour
{
public void GoToLevel()
{
Scene$$anonymous$$anager.LoadScene ("Level 1");
}
}
Then you just drag and drop that gameObject you attached this script to the button component
Oh just realized you found a different solution nice!
Your answer
Follow this Question
Related Questions
Multiple GUI textures one script 2 Answers
Adding picture on top when hovering an object. 0 Answers
Replacing OnMouseEnter/Exit/Down/etc with Raycasting 3 Answers
How to detect mouse leaving / entering the game window? 1 Answer
Can't seem to get OnMouseExit and Destroy(GameObject) to work properly 1 Answer