• 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 /
  • Help Room /
avatar image
0
Question by reinfeldx · Feb 18, 2015 at 07:52 AM · uiinstantiate prefabparent transform

Instantiating UI Prefab - Error: Setting parent of a transform which resides in a prefab is disabled. What's wrong with my code?

I've searched and read docs to no avail. I'm using Unity 4.6.2f1.

The Boss prefab is instantiated from another script; in its start method, it instantiates the BossHealth prefab (a UI Text element).

I know that the BossHealth prefab (being a UI element) should be instantiated as a child of a canvas and I attempt to make that happen in BossHealth's start method. I have a canvas in the scene named Canvas.

In the Inspector, I've set the public GameObject references (they're notated here in my code for clarity).

The script attached to the Boss prefab:

 using UnityEngine;
 using System.Collections;
 
 public class Boss : MonoBehaviour {
 
     public int bossHealth;
     public GameObject bossHealthUI;  // Set in the Inspector. Showing as "BossHealth"
 
     void Start () {

         bossHealth = 5;
         Instantiate (bossHealthUI);
     }
 }

The script attached to the BossHealth prefab:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class BossHealth : MonoBehaviour {

     public Transform canvas;  // Set in the Inspector. Showing as "Canvas (Rect Transform)"

     private Boss boss;
     private Text text;

     void Start () {

         // This line is likely the cause of the error. Why?
         transform.SetParent (canvas, false);

         boss = GameObject.FindGameObjectWithTag ("Boss").GetComponent <Boss> ();
         text = GetComponent <Text> ();
     }
     
     void Update () {

         text.text = "Boss Health: " + boss.bossHealth;
     }
 }
Comment
Add comment · Show 11
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 KiraSensei · Feb 18, 2015 at 10:16 AM 0
Share

Is the object put in the inspector for the variable "canvas" a prefab ?

avatar image reinfeldx · Feb 18, 2015 at 11:45 AM 0
Share

@$$anonymous$$iraSensei - Yes, the canvas object set in the inspector is a prefab.

For what it's worth, the canvas itself was not modified in any way from its default, except that I added three UI prefab children to it (but not the BossHealth prefab, as I want that to be instantiated from a script).

avatar image KiraSensei · Feb 18, 2015 at 12:19 PM 1
Share

So you have your answer :)

your script tries to put canvas as the parent of your current game object, but it is a prefab, and this is not allowed. It has to be a game object already existing in your scene (maybe an instantiation of your prefab canvas).

avatar image Mmmpies · Feb 18, 2015 at 12:37 PM 0
Share

@$$anonymous$$iraSensei is right, no harm in using a prefab but you'd have to instantiate the prefab canvas and give that instance a unique name so you can use GameObject.Find to search for the instantiated canvas once it exists.

Or, as a canvas takes little resources on its own, just leave the canvas in the scene so you can still use your public statement.

avatar image KiraSensei · Feb 18, 2015 at 12:45 PM 0
Share

You are not forced to find it after you create it :

 void Start () {
     GameObject canvasGO = (GameObject) Instantiate(canvas, transform.position, transform.rotation);
 
     transform.SetParent (canvasGO, false);
     ...
 }
avatar image sniper43 · Feb 18, 2015 at 12:55 PM 0
Share

Do you realize if you use

  Instantiate (bossHealthUI);

you have no way to reference this GameObject?

One of these might make your script work:

  this = Instantiate (bossHealthUI);
  transform = Instantiate (bossHealthUI);
  gameObject = Instantiate (bossHealthUI); //LOWERCASE G IS I$$anonymous$$PROTANT





avatar image Mmmpies · Feb 18, 2015 at 02:02 PM 0
Share

True @$$anonymous$$iraSensei but @reinfeldx should really consider changing the way they're instantiating the canvas. At the moment we have the bosshealth prefab trying to set it's parent prefab which is O$$anonymous$$ if that's a known instantiated object or one that's just in the scene and not a prefab.

But to instantiate a panel/slider well any UI element and then instantiate the parent canvas within a script attached to that UI element, well I'm not saying it won't work but what happens if there's another element on that canvas that does the same, you could end up with canvases all over the place.

Or even why not put the bosshealth as part of the canvas that's instantiated so all done in one call.

Not trying to dictate how people build their game, just seems madness to me unless there's a specific reason for it.

avatar image reinfeldx · Feb 18, 2015 at 09:59 PM 0
Share

Edit 2: I've got it working, but I still don't understand the behavior in the beginning of this comment.

I'm understanding now that I can't use a prefab to set the public "canvas" variable in the BossHealth script. However, I have an instance of the canvas already in the scene and I am unable to drop it into the Inspector.

The canvas will be in the scene from the start, so I have no problem trying to reference this instance ins$$anonymous$$d of the prefab. I'm just not sure why I can't set it in the Inspector. Edit 1: $$anonymous$$aybe I should note that I am trying to edit the BossHealth prefab directly in the prefab folder.

alt text

At this point I've also tried changing my declaration for canvas to a GameObject ins$$anonymous$$d of a Transform, like so:

     public GameObject canvas;
 
     void Start () {
         transform.SetParent (canvas.transform, false);

So now the Inspector is asking for a GameObject ins$$anonymous$$d of a Transform, but I am still unable to drag the canvas into it.

To answer @$$anonymous$$mmpies's question about why I'm not including BossHealth in the canvas that's instantiated - it's because I would like to only show BossHealth when the Boss is in the scene. The rest of the canvas includes UI elements that should be shown constantly, like Score and Lives.

Edit 2: The following approach worked for me. Setting it via code ins$$anonymous$$d of setting it in the Inspector after tagging the canvas.

     private GameObject canvas;
 
     void Start () {
         canvas = GameObject.FindGameObjectWithTag ("Canvas");
         transform.SetParent (canvas.transform, false);

I'd still like to know why the Inspector approach wasn't working.

screenshot-20120218.png (84.8 kB)
avatar image KiraSensei · Feb 18, 2015 at 10:32 PM 0
Share

So declare canvas as a game object in your script :)

 public GameObject canvas;
avatar image reinfeldx · Feb 18, 2015 at 10:40 PM 0
Share

@$$anonymous$$iraSensei - Thanks, but I tried that. See 2/3 of the way down in my previous comment.

avatar image KiraSensei · Feb 18, 2015 at 10:47 PM 0
Share

Oh, didn't see that, sorry.

There is no reason it should not work :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Little_big · Sep 12, 2016 at 01:08 AM

GameObject convasGo = (Instantiate(panel, transform.position, transform.rotation, this.gameObject.transform) as GameObject);

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Clone of clones 2 Answers

instantiated projectile conflicts 0 Answers

How to copy GameObject and preserve Prefab connection and Component values from editor script 2 Answers

Making a time bomb 1 Answer

Problems with instantiated objects in a circle formation 0 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