Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kadeo · Jun 09, 2016 at 02:34 PM · scene-switchingmouseoveronmouseenteronmouseexit

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;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kadeo · Jun 09, 2016 at 05:23 PM 0
Share

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?

avatar image DizzyWascal Kadeo · Jun 10, 2016 at 01:01 AM 0
Share

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

avatar image DizzyWascal Kadeo · Jun 10, 2016 at 01:04 AM 0
Share

Oh just realized you found a different solution nice!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges