• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vlePa · Oct 17, 2011 at 03:09 PM · guiinstantiateprefabs

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image save · Oct 17, 2011 at 03:13 PM 0
Share

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`).

avatar image vlePa · Oct 17, 2011 at 04:03 PM 0
Share

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.

avatar image Bunny83 · Oct 17, 2011 at 04:33 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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...)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vlePa · Oct 17, 2011 at 08:34 PM 0
Share

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.

avatar image Bunny83 · Oct 17, 2011 at 11:20 PM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to apply check on an instanced prefab 2 Answers

change prefabs with Gui.buttons 0 Answers

How would I go about making random car prefabs drive up and down a road at varying speeds? (for example, crossy road) 1 Answer

Wrong coordinates of setting position of the object 1 Answer

Custom Inspector GUI, Instantiating some classes 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges