I’m running into a weird issue with AI character spawning. I created a prefab that works perfectly when I drag it into the scene through the Unity editor. However, when I try to instantiate the same prefab during gameplay using code, the AI behavior gets messed up.
The main problem is with the ThirdPersonCharacter component on my AI. When spawned at runtime, the character gets stuck in idle animation and won’t transition to walking. I traced it down to the CheckGroundStatus method where this line causes issues:
characterAnimator.applyRootMotion = false;
Instead of playing the walk animation properly, the character just slides across the ground to reach its target. It seems like the ground detection isn’t working when the prefab is spawned dynamically.
What could be different between the editor-placed version and the runtime-spawned version of the exact same prefab? I’m using Unity’s standard AI setup and this has me stumped.
Had the exact same problem with AI prefabs. Unity’s NavMeshAgent doesn’t properly connect to the NavMesh surface when you spawn at runtime - editor objects get time to settle, but runtime spawning creates a gap between the agent’s position and the walkable surface. Here’s what worked: call NavMeshAgent.Warp() right after spawning, using the spawn position projected with NavMesh.SamplePosition(). This locks the agent onto the nav mesh before it starts moving. That sliding you’re seeing happens when root motion gets disabled but the NavMeshAgent’s still trying to move without proper ground contact.
Sounds like a timing issue with component initialization. Editor-placed prefabs get fully initialized before the scene starts, but runtime spawning creates race conditions between systems. I had the same problem - my AI characters would spawn but their NavMeshAgent wasn’t syncing with the Animator properly. Your ground detection probably fails because the character controller or rigidbody hasn’t settled when CheckGroundStatus runs. Try adding a small delay before enabling AI behavior after spawning. Use a coroutine that waits a frame or two, then force a ground check. Also check if your spawned prefab is actually touching the ground - runtime spawning sometimes places objects slightly above the surface, breaking detection until physics catches up.