- Home /
Make UI elements invisible
I need to make a UI button invisible, but not by the command SetActive(false) because I need a script on it to run. I tried below commands but none of them works, seems they are obsolete or wrong
UIButton.renderer.enabled=false;
UIButton.CanvasRenderer.enabled=false;
UIButton.GetComponent(CanvasRenderer).enabled=false;
UIButton.GetComponent(Renderer).enabled=false;
It seems there is no enabled property for renderer or canvasRenderer in Unity 5. any solutions?
BTW if you want an "invisible (working) button" .. here:
Answer by DiegoSLTS · May 21, 2015 at 12:46 AM
I usually do this to hide UI stuff:
Add a Canvas Group component to the root object you want to hide (just the root, the childs don't need the component)
Setup a reference to that Canvas Group on the script that should hide the UI element.
Say you named "canvasGroup" the reference to the Canvas Group component, then call this code whenever you want to hide it.
void Hide() {
canvasGroup.alpha = 0f; //this makes everything transparent
canvasGroup.blocksRaycasts = false; //this prevents the UI element to receive input events
}
Do the opposite to show it again
void Show() {
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
}
How do you make a reference on the script? @DiegoSLTS,How do you set a reference?
There are multiple ways to do this. The easiest is to have a public or serialized member on your script (in my example, a member called "canvasGroup" of type CanvasGroup) and drag and drop a game object from the editor with a CanvasGroup component on the field you see in the inspector. Another common approach is using the "Find" or a similar method to get the game object you want from the scene and assign it to the "canvasGroup" member.
Adding a CanvasGroup to an InputField object makes it uninteractable. I tried it, and I can no longer enter any values.
Very helpful - exactly what I needed since I have UI windows with scripts attached that need to be active at all times. Thank You!
Answer by jrjr · May 21, 2015 at 01:01 AM
Set scale to 0,0,0 also works in most circumstances (can cause some issues if you've got complex 3d objects in a world camera usage though). You just set it back to its live scale whenever you want it 'enabled'.
I think this is a neat, clever and super simple solution, I'm fairly new Unity but imagine this very low performance impact, I'm using it for a points display which I want to hide at certain times. For newbies, possible code like: Vector3 blessScale = blessingPoints.GetComponent().localScale; blessScale = new Vector3(1,1,1); blessingPoints.GetComponent().localScale = blessScale;
then to hide it, same as above with (0,0,0) ins$$anonymous$$d. if a solution this simple will do, no need to create extra methods or object holders.
Answer by DarthHawk13 · Dec 26, 2016 at 09:46 PM
I created a Youtube video which answers this question
https://www.youtube.com/watch?v=iwxWrx98RfI
Essentially you add a Canvas Group component to the Canvas with the controls you want to make invisible and access the Canvas Group in code and make it invisible and/or not interactive.
CanvasGroup.alpha = 0f;
CanvasGrouop.interactable = false;
CanvasGroup.blocksRaycasts = false;
You can have multiple Canvas objects with different controls occupy the same area on the screen.
Answer by cjdev · May 20, 2015 at 10:45 PM
You could try making a new tag assigned to the buttons you want to be invisible and excluding them in a custom layer. If you use that layer on your camera it should render everything but the UI elements with the new tag.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200107im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Renderer.enable for GameObject[]? 3 Answers
animation.speed does not work when disabling/enabling renderer 1 Answer
Gameobject with many child objects disable mesh not working. 0 Answers
Keypress.ismine? 0 Answers
Cannot select Animation Event 3 Answers