- Home /
Instantiating unique Prefabs to unique child objects in C#
/*I'm trying to instantiate multiple prefab "doors" to multiple hallway sections in the scene. Each hallway section has it's own child object that serves as potential spawn point for a prefab "door". I can spawn multiple prefab "doors" to the same child object of one hallway section, but can't get prefab doors to spawn for child objects of multiple hallway sections. My goal is to generate a unique prefab door for each hallway section in the scene. Any help and or feedback would be fantastic...My code is below:*/
for (int i = 0; i < currSections.Count; i++)
{
// Identifying the first section that needs a ceiling door.
GameObject activeSect = currSections [i];
// Print the section that is chosen.
Debug.Log ("Active section: " + activeSect);
// Identifies the child of the section that will be the spawn point for the ceiling door.
GameObject ceilingSpawn = activeSect.transform.GetChild (1).gameObject;
// Add the ceiling door spawn point to a list.
ceilingSpawnPts.Add (ceilingSpawn);
// Print the gameObject chosen for the ceiling door spawn point.
Debug.Log ("The spawn point is " + ceilingSpawnPts [i]);
// Chooses a random ceiling door from a list.
int ceilIndex = Random.Range (0, ceilingDoors.Count);
// Identify the chosen ceiling door gameObject.
GameObject currCeil = ceilingDoors [ceilIndex].gameObject;
// Print the name of the active GameObject.
Debug.Log ("The chosen ceiling door is " + currCeil);
Debug.Log ("i = " + i);
// Add the chosen ceiling door to the current ceilings list.
currCeilings.Add (currCeil);
// Print the name of the active GameObject.
Debug.Log ("The added ceiling door is " + currCeil);
// Clones the ceiling door GameObject and places it into the scene at the spawn point.
GameObject ceilDoor = Instantiate (currCeil, ceilingSpawn.transform.position, Quaternion.identity) as GameObject;
}
Answer by seekerG · Jul 11, 2018 at 05:08 PM
Yeah, sorry for the verbosity. Thanks for your help. I tried that but received a "Setting the parent of a transform which resides in a prefab.." error. I realized that the hallway sections are also prefabs, so I have a nested prefab problem. I'm going to research some of the plugins available, but please let me know if you have a better solution. Thanks again!
I see. The nested prefab problem is just an issue with creating prefabs not using them. Have you instantiated the hallway sections first and are referencing those instances ins$$anonymous$$d of just their prefabs? Sorry if you know this and this isn't the problem. Otherwise I would have the hallway sections create their own doors and add themselves to a list in the parent if you need reference to them.
Thanks again. I'm pretty new to program$$anonymous$$g, so most of this is new territory. Initially, the hallway sections were instantiated first and then the doors spawned to their child gameObjects. But the hallway sections were already prefabs, so the nested error occurred when I tried to set their parent. I haven't quite worked out the hallway sections creating their own doors yet. Also experimenting with new GameObjects to reference as parent objects.
Answer by $$anonymous$$ · Jul 11, 2018 at 01:31 PM
That is quite verbose. At a glance it looks like you need to set the parent transform in your prefab instantiation to ceilingSpawn.transform.
Answer by luiscmendez · Jul 11, 2018 at 09:51 PM
I added this line at the end of that method and it seemed to work just fine for me. Let me know if that's not the desired functionality:
ceilDoor.transform.parent = ceilingSpawn.transform;