- Home /
Unity 2019.4.9f1 - Toggle Show/Hide Gameobject with one keystroke.
Hi all.
I have constructed a spiral freezer and want to simulate automated cleaning on it.
To do that I am making 9 "segments", each consisting of an empty gameobject with several particle systems as children.
I would like to start with all segments hidden og then be able show/hide each segment when pressing keys 1-9.
By searching the web I found several answers, but all seem to be outdated?
Can anyone help me code show/hide a gameobject+children in one keystroke?
Answer by Namey5 · Sep 16, 2020 at 06:07 AM
[SerializeField] private KeyCode m_Key = KeyCode.G;
[SerializeField] private GameObject m_Target;
private void Update ()
{
if (m_Target != null && Input.GetKeyDown (m_Key))
m_Target.SetActive (!m_Target.activeSelf);
}
Basic idea is to wait for the key press, then set the object active to the opposite of its current state.
Thx Namey5. <3
But there is a problem.
The gameobject gets hidden by pressing "G", but pressing "G" again does not make it show/ reappear again.
'm_Target' cannot be the same object the script is attached to - an object can't receive input if it's disabled.
Wait. I tested it on a sphere.
On the Segments it works.
Thank you again. $$anonymous$$uch appreciated :)
Your answer
Follow this Question
Related Questions
Object Visibility 2 Answers
Objects Appearing 1 Answer
Hide Object in Editor Only 5 Answers
Can you use renderer.enabled on array members ? 3 Answers
Show/hide GUI texture 2 Answers