- Home /
Managing gui with prefabs
I want to instantiate prefabs in runtime that hold certain GUI code on them. When I instantiate them, the GUI doesn't show as opposed to when manually dragged from project view to the scene.
Why is that? Maybe there is another way im not aware of.
Thank you.
What kind of GUI code do you have? How is it different? If the component or your code reference to a position you'd have to keep the same Vector3-position when instantiating as when you drag them to the scene (`Instantiate(the object, prefabs Vector3 position, Quaternion.idendity`).
I'm using onGUI() function. It's an empty game object with a script attached.
I worked out a solution with GUI.Window
The question remains, though, why won't an instantiated prefab show it's OnGUI() elements.
Don't post your comments as answers! I've converted it to a comment. Also if you want to change something use the edit button.
Answer by Bunny83 · Oct 17, 2011 at 04:31 PM
It definitely works with instantiated prefabs. I guess you forgot to use unique window IDs for your GUI.Window calls. The IDs have to be project-wide unique. You can't draw two windows with the same ID. Usually if you have a quite complex system you use a manager to apply the IDs.
A very simple quick and dirty manager would be something like that:
//C#
public class WindowIDManager
{
private static int m_NextWindowID = 0;
public static int GetWindowID()
{
return m_NextWindowID++;
}
}
In all classes that requires a windowID just add a variable to hold the window id for this instance and set the id in Awake().
//C#
private int m_WindowID;
public void Awake()
{
m_WindowID = WindowIDManager.GetWindowID();
}
GetWindowID will never return the same ID (well at least for the next 2147483647 IDs...)
You misunderstood my last comment. I worked out a solution using windows which eli$$anonymous$$ated the need for prefabs (and their respective bugs in my case).
Still, I didn't manage to instantiate a prefab and make it to show its OnGUI() elements. Hope I made it a bit clearer now.
Thanks for your replies.
Well, i still can't reproduce the behaviour you mentioned. I have a lot of objects that get instantiated at runtime and contain OnGUI stuff. Everything works as expected. Can you post a small example script which does not work for you?
Answer by vlePa · Oct 20, 2011 at 05:36 PM
Problem solved. It was due to GUI.depth parameter. The gui we wanted was behind the fullscreen background from another gui.