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.