Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Brood · Jan 07, 2014 at 03:15 AM · setactiveonmouseenter

How to make a gameObject disappear when a mouse hovers and reappear when the mouse leaves?

Greetings Unity Answers, this is my first time asking you.

The situation is:

  • There is a cube (gameObject).

  • If a mouse hovers over it, the cube disappears.

  • If the mouse leaves, the cube reappears.

This is a script I attach to the parent of the gameObject I want to make appear/disappear:

 public class Visibility2 : MonoBehaviour 
     {
         void OnMouseEnter ()
         {
             foreach (Transform child in this.transform)
             {
                 child.gameObject.SetActive (false);
             }
         }
     
         void OnMouseExit ()
         {
             foreach (Transform child in this.transform)
             {
                 child.gameObject.SetActive (true);
             }        
         }
     }

I eventually got the basics right.

i.e Able to use setActive function using keyboard input or mouse button input and attach it to a parent so that (true/false) functions properly.

But the purpose of my question is to find out how to apply an OnMouseEnter/Exit-like input on a setActive function.

There is also a similar script that is also plausible:

 public class Visibility : MonoBehaviour 
 {
     void Update () 
     {
         foreach (Transform child in this.transform)
         {
             if (Input.GetMouseButtonDown(0)) 
             {
                 child.gameObject.SetActive(false);
             }
             else if (Input.GetMouseButtonUp(0)) 
             {
                 child.gameObject.SetActive(true);  
             }
         }
     }
 }

Except that I want to hover with a mouse ,not press a button on a mouse, but as far as I know, there are no substitute Input functions that provides hovering (e.g like OnMouseEnter/OnMouseExit), but maybe this is my best idea I can only get.

It's possible that I miss something or I am doing it wrong, but I am at a dead-end here so if you are able a, if not a full answer, at least a suggestion, that would be great, thanks.

Comment
Add comment · Show 1
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 ShadoX · Jan 07, 2014 at 07:24 AM 0
Share

Why not just use a Ray to get the gameObjects info ?

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 100)){
 //...

and then just get the collider / gameObject from the ray

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Brood · Jan 07, 2014 at 11:00 AM

I''d like to confirm that I was able to solve the problem myself already, and that I shouldn't jump to conclusions.

The Visibility2 script actually works, and the issue I ran to is that I hadn't apply a collider to the parent, which is absolutely necessary in order for OnMouseEnter/Exit to work.

Thanks for the suggestions.

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

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

physics.OverlapSphere colliders 1 Answer

Keydown and OnMouseEnter conflict? 0 Answers

Error with picking up an item... 1 Answer

OnMouseEnter/Exit not working when pressing button to advance scene 2 Answers

Unable to reactive the game object 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