Question by
tall-josh · Jul 14, 2017 at 10:30 AM ·
scripting problemchildinheritancechild-to-gameobjectheirarchy
Recursive SetParent not working
Hi, I'm trying to recursively construct a character from a list of prefabs. However, when I use either SetParent
or Instantiate(new_game_object, parent)
by passing the parent via a method parameter the parent is not set.
This method.;
void InstantiateConfiguration(ConfigurableElement config_data, List component_list)
{
GameObject new_config = new GameObject();
new_config.name = config_data.Name;
new_config.AddComponent<Configurator>();
new_config.GetComponent<Configurator>().Configure(config_data, new_config, component_list);
}
invokes this method;
public void Configure(ConfigurableElement config_data, GameObject parent, List<GameObject> component_list)
{
try
{
string rand_part = config_data.ValidParts.SelectRandom().Name;
GameObject new_go = Instantiate(component_list.Find(p => p.name == rand_part));
new_go.transform.SetParent(parent.transform, false);
}
catch (System.NullReferenceException)
{
//You done goofed
}
// Instantiate Children
if(config_data.Children != null)
{
foreach (ConfigurableElement child in config_data.Children)
{
GameObject c = new GameObject();
c.name = child.Name;
c.AddComponent<Configurator>();
c.GetComponent<Configurator>().Configure(child, c, component_list);
}
}
I can make it work if I do everything in one monolithic method, but I'm sure there's a better way. Cheers in advance.
Josh
Comment
Answer by tall-josh · Jul 14, 2017 at 11:05 AM
Ahhhhh STOP PANICHING EVERYONE!!!! I found it...I forgot c.transfrom.SetParent(parent.transform)
on line 20.