0
votes

I've made and animated a player sprite in one unity scene and saved the gameobject as a prefab. In the scene I created it it animates wonderfully, but whenever I try the prefab in another scene it breaks. After some debugging it appears that the Animator component is null in the other scenes, and has relevant data in the original scene. The following is what I'm using to get a reference to the animator in code that works in the original scene but not in the others.

private Animator myAnim; 

void start(){
    myAnim.getComponent<Animator>();
    if(myAnim != null)
    {
        print("myAnim Exists"); //this happens in the original scene but not in the others
    }
}

void update(){
    myAnim.SetFloat("moveX", myRB.velocity.x); //where the red error points to at runtime. Says an object reference doesn't exist
}
1
Is the animator wired up properly in your prefab? - ZombieTfk
I believe so. I can delete the prefab and drag it back into the original scene and it works, and the prefab in the scene has no changes to apply to the prefab in the project folders - Justin Dickinson

1 Answers

0
votes

The player had some other prefabs that weren't related to the error code that it needed to reference in the other scenes that didn't exist outside of the original scene. Idk why the error lead me on a wild goose chase but at least it's solved now.