Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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);
     ...
 }
Show more comments

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.

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

Making a time bomb 1 Answer

Problems with instantiated objects in a circle formation 0 Answers

SetParent changing the relative position of the parent 1 Answer

Clone of clones 2 Answers

instantiated projectile conflicts 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