Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by Owlglass_Moot · Jul 11, 2015 at 07:14 AM · uierrorinstantiateprefabstart

Start() of Instantiated Prefabs Not Being Carried Out?

Hello! I'm having a great deal of trouble with a "chain" of instantiated GameObjects.

I have four prefabs (TribesContainerPrefab, TribePanelPrefab, TribeBackgroundPrefab, and TribeSummaryPrefab). Each prefab has an appropriately named script attached. In the "TribesContainer" Start function, three TribePanelPrefabs are initiated. In the "TribePanel" Start function, one TribeBackgroundPrefab and one TribeSummaryPrefab are initiated. So the resulting hierarchy should look something like this:

TribeContainer
. . .TribePanel
. . . . . . TribeBackground
. . . . . . TribeSummary
. . .TribePanel
. . . . . . TribeBackground
. . . . . . TribeSummary
. . .TribePanel
. . . . . . TribeBackground
. . . . . . TribeSummary

Here are (simplified versions of) the classes in question:

TRIBES CONTAINER


 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 using System.Collections;
 
 public class TribesContainer : MonoBehaviour {
 
     void Start ()
     {
         gameObject.name = "TribesContainer";
         gameObject.transform.SetParent(GameObject.Find("MainCanvas").transform,false);
         
         // The value of "initializedTribeCount" is 3.
         for (int i=0; i<TribeManager.instance.initializedTribeCount; i++)
         {
             var go = Instantiate(GameManager.instance.tribePanelPrefab);
             go.transform.SetParent(gameObject.transform, false);
         }
     }
     
 }


TRIBE PANEL


 using UnityEngine;
 using System.Collections.Generic;
 
 public class TribePanel : MonoBehaviour {
 
     void Start()
     {
         gameObject.name = "TribePanel";
         var go = Instantiate(GameManager.instance.tribeBackgroundPrefab);
         go.transform.SetParent(gameObject.transform, false);
         var ho = Instantiate(GameManager.instance.tribeSummaryPrefab);
         ho.transform.SetParent(gameObject.transform, false);
 
     }
 }
 

TRIBE BACKGROUND


 using UnityEngine;
 using System.Collections.Generic;
 
 public class TribeBackground : MonoBehaviour {
     
     void Start()
     {
         gameObject.name = "TribeBackground";
     }
 
 }
 

TRIBE SUMMARY


 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TribeSummary : MonoBehaviour {
 
     void Start()
     {
         gameObject.name = "TribeSummary";
     }
 }



Everything instantiates perfectly, everything is displayed and organized correctly in the hierarchy, and everything is positioned properly. But trying to add any additional code to the Start() of TribesContainer reveals that (beyond their basic instantiation) TribePanel's Start() does not seem to be carried out.

For example, adding the following to TribesContainer.Start():

         Transform[] x = GetComponentsInChildren<Transform>();
         foreach (Transform child in x)
         {
             Debug.Log(child.name);
         }

Outputs:

         TribesContainer
         TribePanelPrefab(Clone)
         TribePanelPrefab(Clone)
         TribePanelPrefab(Clone)


And adding:

         Debug.Log(transform.GetChild(0).name);
         Debug.Log(transform.GetChild(0).GetChild(0).name);

Outputs:

         TribePanelPrefabClone()
         UnityException: Transform child out of bounds


I found this answer this answer which suggested that I needed to associate the prefabs through the editor or through Awake(), but neither worked. I'm sure it's something stupidly obvious that I'm not getting, and it's driving me bonkers.

Cheers to anyone who can help! And let me know if there's anything I need to clarify.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owlglass_Moot · Jul 11, 2015 at 06:08 AM

And of course as soon as I hit submit, a light bulb flicks on in my head and I figure out the problem. :I

I needed to make the instantiations in Awake() rather than Start(). Although I'm still not 100% sure why.

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

2 People are following this question.

avatar image avatar image

Related Questions

Setting the parent of a transform which resides in a prefab error 1 Answer

Instantiated gameObject(here, wall) destroyed, but when checked for existence, shows that it exists, how? 1 Answer

How to change Image on button click in a prefab that is instantiated 1 Answer

Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer

Layout Group not working when instantiate from prefab 1 Answer


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