Object invisible until mouse hovers button (592656)

I tried this thread already in the OnGUI forum but didn´t got any response there. So i try it here again.

It did sound very simple to me in the beginning…but somehow i mixed it up too much.
I have a scene with a few buttons and some objects.
I want that when i mouse hover over button1…object1 appears…otherwise the object1 is invisible.

    using UnityEngine;
    using System.Collections;
    
    public class ShowTown : MonoBehaviour {
    
        public GameObject place = null;
    
        private void OnMouseOver()
        {
            if (place != null && place.gameObject != null) {
                place.gameObject.SetActive(true) = place.gameObject.SetActive(false);
            }
        }
    }

somehow i cant say like..

place.gameObject.enabled = !place.gameObject.enabled;

Anyone any idea what i did wrong here?

// toggle
place.gameObject.SetActive(!place.gameObject.activeSelf);

// set true
place.gameObject.SetActive(true);

// set false
place.gameObject.SetActive(false);

you can’t assign a value to a function call…

if you want it to appear when you mouse over, and disappear when you move the mouse away you want to set it true in the OnMouseOver and false in the OnMouseExit functions

Thank you for the help so far, and that i strugle here somehow so much.

using UnityEngine;
using System.Collections;

public class ShowBonn : MonoBehaviour {
   
    public GameObject place = null;
   
    private void OnMouseOver()
    {
        if (place != null && place.gameObject != null) {
            place.gameObject.SetActive(true);

        }
    }

    private void OnMouseExit() {
        if (place != null && place.gameObject != null) {
            place.gameObject.SetActive (false);
        }

You mean like this?
But where do i have to use the toggle now?

Nowhere, he was giving an example.

1 Like

yup, the toggle was just an example to satisfy

it also highlights the attribute (activeSelf) vs function (SetActive(…))

it’s not involved in what you are trying to do as you have explained it

Thanks for all the help , at least i dont get any compile errors anymore. :smile: It currently still doesn´t work , and stays on all the time…but i hope i figure that out very soon.

OnMouseOver is actually supposed to be OnMouseEnter I think (unless you want it to get called every single frame that the mouse is over the control, rather than just the first frame), and I’m not sure they’re allowed to be private. You might also be missing a closing brace, possibly.

Unity ignores private modifiers on the magic methods, because it can.

You might be better off implementing this as EventSystem interfaces.

1 Like