- Home /
Unity 4.6 UI - Accessing a Panel from a Script?
Seems like the Panel is the only UI Component you can't access from a script, am I wrong? Am I missing it? Is it not called a Panel?
I need a simple way to manage a group of UI Components ( Similar to an online games interface popping up ), and have the ability to enable/disable them instantly.
Enabling/Disabling them one at a time is a mess, and grouping them is just so much easier.
Answer by Kiwasi · Nov 22, 2014 at 10:06 AM
There is no such thing as a panel component. The panel created from the UI menu is a GameObject with a RectTransform and an Image component attached. You can access any of these via script. It sounds like what you are looking for is simply GameObject.SetActive()
The problems I've always run into with SetActive() is that once you call it from a script, enabling that game object from the same script has become impossible, has it throws reference exceptions.
It works fine if you keep a reference to the inactive object. The code that's causing you problems is one if the Find methods. As a rule these only return active objects.
Answer by coding_crow · Feb 16, 2015 at 09:29 AM
Look into Canvas Groups, I was wondering the same thing and they seem to do what we thought the panel was for.
Answer by BonusB · Jul 03, 2020 at 08:43 AM
I know I'm like 6 years late but, how do I reference a panel via script without using public variables? because prefabs can't use public variables.
I'm not 100% sure what you're asking, so I'll answer a few variations of that question. If you want to have a reference to a prefab in a script, you can put a public GameObject there and drag-and-drop the prefab from the Asset menu into the slot in the inspector, just like you can drag-and-drop from the Hierarchy menu. Unity's guide for this is at https://docs.unity3d.com/$$anonymous$$anual/InstantiatingPrefabs.html. For another variant of the question - scripts in prefabs can have public variables,