Unity AI prefab works in scene but has animation issues when instantiated at runtime

I’m running into a weird issue with Unity AI characters. When I drag my AI prefab directly into the scene through the editor, everything works perfectly fine. The character moves around, animations play correctly, and there are no problems at all.

But when I try to instantiate that same prefab during gameplay using code, something goes wrong with the animations. The AI agent gets stuck playing the idle animation and won’t transition to walking animations properly. Instead of walking normally to its destination, the character just slides across the ground.

I think the issue is happening in the ground detection logic. There’s a function that checks if the character is touching the ground, and it seems like this isn’t working right for runtime-spawned objects. Here’s the code that’s causing trouble:

characterAnimator.applyRootMotion = false;

This line should handle the movement animations, but it’s not behaving the same way for spawned objects versus editor-placed ones. The character can’t seem to detect the ground properly when spawned at runtime.

I’m using Unity’s standard third person character controller. Has anyone else run into this before? What could be different about the same prefab when it’s spawned versus placed in the editor?

This happens because the Animator state machine doesn’t sync with the NavMeshAgent when you spawn it at runtime. The root motion and agent velocity get out of sync, which causes that sliding effect. I fixed this by resetting the Animator after spawning. Right after instantiation, call characterAnimator.Rebind() then characterAnimator.Update(0f). This forces the Animator to reinitialize and sync with the NavMeshAgent. Also check if your ground detection script uses Physics.Raycast in FixedUpdate vs Update. Runtime spawned objects need at least one physics tick to settle. If your ground check runs before that, it’ll return false results and mess up the animation transitions.

Check your ground detection raycast length and layer masks. I’ve seen this mess up runtime spawns constantly.

Your prefab works in the editor because everything loads together. At runtime, the character controller and rigidbody aren’t fully settled on the ground yet.

Try forcing the character position down after spawning:

var spawnedChar = Instantiate(aiPrefab, spawnPosition, spawnRotation);
spawnedChar.transform.position = new Vector3(spawnPosition.x, spawnPosition.y - 0.1f, spawnPosition.z);

Make sure your ground detection uses the right layer mask. Your ground check’s probably failing because the raycast isn’t hitting what it expects.

Also try disabling the NavMeshAgent for one frame after spawning, then re-enable it. Forces it to recalculate its position properly.

I’ve hit this exact issue before. It’s usually a timing problem - when you spawn a prefab at runtime, components don’t initialize in the same order as they do in the editor. Your Animator might be firing up before your ground detection script sets its references or before the NavMeshAgent connects to the navmesh. Quick fix: add a small delay after spawning before you enable the Animator or AI components. Use a coroutine with ‘yield return null’ to wait one frame, then turn on your movement logic. Also check that your spawned prefab is actually sitting on the navmesh surface - runtime spawning sometimes drops objects slightly off the navmesh and breaks everything.

had the same weird issue with my AI. the navmesh agent gets confused between warping and regular movement when you spawn at runtime. right after instantiating, set agent.isStopped = true, then flip it back to false after a couple frames. forces the navmesh to recalculate and usually fixes those sliding/animation sync problems.