Show and Hide a button when object is clicked per mouse

Hello,

I have a button I cannot hide (it is always on Screen but I want to hide it and then Show it)
I have a houseobject and if i click on this house I want my button do appear on the Screen.

my script on the houseobject so far is:

void OnMouseDown ()

{
	Debug.Log ("houseclicked");

}

Shall I parent the button to the houseobject?

Thanks for reading,
cheers

Hi @Flowered,

Sorry I missed that you’d responded to me. Assuming this is the only button on the Canvas do this. As this is the new UI…

Add a CanvasGroup to the Canvas the button is on and un-tick interactable and blocksRaycast also set alpha to 0. The button is now invisible.

In the script where you click the house and then want the button to appear alter it to this:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class asdf : MonoBehaviour {

    public CanvasGroup myCG;//Its your Canvas

    void OnMouseDouwn()
    {
        Debug.Log("Pressed left click.");
        myCG.interactable(true);
        myCG.blocksRaycast(true);
        myCG.alpha = 1;
    }
}

Or just set the Canvas to active:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class asdf : MonoBehaviour {
    
        public Canvas myCanvas;//Its your Canvas
    
        void OnMouseDouwn()
        {
            Debug.Log("Pressed left click.");
            myCanvas.SetActive(true);
        }
    }

If you want the canvas active but just want to hide the button then you can use the CanvasGroup method but just put the CanvasGroup on the button itself.

There are lots of options for doing this so don’t feel you have to stick with what I put I think really you just need to learn how to access the components on the new UI.

EDIT

And this @Flowered is why I shouldn’t type stuff on my phone!

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class MenuOnClick : MonoBehaviour {
		
	public Canvas myCanvas;//Its your Canvas
	
	void OnMouseDown()
	{
		Debug.Log("Pressed left click.");
		myCanvas.gameObject.SetActive(true);
	}
}

Put this on your house object and put your Canvas in the slot for the script and make sure you un-tick the Active button on the canvas:

40525-canvasnotactive.png

That should work for you.

EDIT 2

and if you want to use the CanvasGroup method:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class MenuOnClick2 : MonoBehaviour {
	
	public CanvasGroup myCG;//Its your Canvas
	
	void OnMouseDown()
	{
		Debug.Log("Pressed left click.");
		myCG.interactable = true;
		myCG.blocksRaycasts = true;
		myCG.alpha = 1;
	}
}

But make sure the canvas is active as the CanvasGroup will take care of making it visible and interactable.

Always get the = true and (true) wrong! Sorry @Flowered.

Hi,
I have attached the script.Initially your button should be inactive in hierarchy. Now on clicking mouse it will get active on screen. `using

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class asdf : MonoBehaviour {

	public GameObject abc;//Its your button
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () 
	{
	
		if(Input.GetMouseButtonDown(0))
		{
			Debug.Log("Pressed left click.");
			abc.SetActive(true);
		}
	}
}

Hope this will help you. You can place the code in start as per your need.