- Home /
Prefab Instantiate children transforming to wrong position
I have a prefab which is a empty game object with an NPC and a chair as its children.
I created a test scene with a simple ground and was able to instantiate a prefab and set its position and rotation to game object in the scene. I was able to move/rotate that game object and the prefab was created and moved to the location I expected.
I took the same script and the same prefab and placed it in a more complicated scene. The chair, which has no children or animations was placed in the correct location. The NPC, which has an Animator, a script calling a Animator trigger, and lots of children appears near 0,0,0 but not quite at it. No matter where I place the spawn object the NPC always appears at the same location in the same complicated scene.
Both scenes have NavMesh generated.
GameObject spawn = GameObject.Find("spawn_here");
if (spawn != null){
GameObject prefab = Instantiate(Resources.Load("prefab") as GameObject);
prefab.transform.position = spawn.transform.position;
prefab.transform.rotation = spawn.transform.rotation;
}
Thank you!
Answer by RobotWarden · Aug 28, 2017 at 08:03 PM
Temp Solution:
I removed the NavMesh agent off the NPC in the Prefab and the NPC was placed in the correct location. Right now I don't need this NPC to move so it is a fine solution for this one scene. It seems like in the future I will want to spawn the NPC separate from the chair they sit in and have them walk to the chair then sit down as the scene starts.