- Home /
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
It is a new 4.6 GUI Button. Do you know how to hide it until I click on the houseobject to let it appear on Screen ?
Thank you
@Flowered Disable/Enable the Canvas in which the button is in. Or, create a script with a public Button object, drag and drop the button gameObject from the hierarchy, and then do what you need to do.
Answer by Mmmpies · Feb 10, 2015 at 09:05 AM
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:
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.
Hello $$anonymous$$mmpies,
Unity telling me this when trying your script;
Assets/$$anonymous$$enuPopup.cs(22,24): error CS1955: The member `UnityEngine.CanvasGroup.interactable' cannot be used as method or delegate
Why does Unity do this? I'm depressed. Thanks for your time by the way :-)
I have to thank you very much. Thank you for your time. &' btw, don't type on your phone again! - smiles :-))))
When I want to remove all Buildings off my Hierarchy and get the CanvasGroup into my Prefab (which I want to instantiate), it is not possible to drag the Canvas into the inspector and attach it to the script.
It is only working when I have objects in my Hierarchy and do all the stuff there. I cannot use it with prefabs. Do you have any idea ?
Answer by shriya · Feb 09, 2015 at 06:00 AM
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.