Animation override stopped working in the following lecture

Hello friends,

When I followed the Animator Override Controller lecture initially, the animation did override as intended. However, when we did the refactoring to move things into the weapon scriptable object, it stopped working. The sword instantiates as expected and has its expected stats as set in the Sword asset, but the animation remains as the default.

Here are the relevant pieces of code and screenshots to show how things are hooked up in the editor. Thankfully I can move on without this being resolved, but it bothers me that I can’t figure out what I’ve done wrong in the refactoring.

image
image

From Weapon.cs:

public void Spawn(Transform handTransform)
{
if (equippedPrefab != null)
{
Instantiate(equippedPrefab, handTransform);
}

        if (animatorOverride != null)
        {
            Animator animator = FindObjectOfType<Animator>();
            animator.runtimeAnimatorController = animatorOverride;
        }
    }

Fighter.cs doesn’t pass an animator in since it looked like we could find the object from within the Weapon.sc anyway. Seemed like it would streamline that process, but is that what’s breaking it? If so, why would it? I have stared at this code for almost an hour tracing the process with no luck finding where and why it breaks. I get no errors. Some connection is somehow missing, and I’m putting my hands up.

Appreciate any insights. Thanks!

In typical fashion, the moment I ask for help, I see the problem. I am using FindObjectOfType instead of grabbing the component. This is also why the animator had to be captured in Fighter.cs and passed into the weapon asset; the weapon asset does not have an animator component, and it’s not located as an object in the scene to be found directly.

A couple broken ribs and a bruised ego, but we got it working!

image

Maybe this will help someone else in the future!

Excellent job working that out!
ScriptableObjects aren’t tied to a GameObject in the same way that MonoBehaviours are. This means we have to pass that data on to the SO for it to work on.

Technically, since you had a reference to the hand transform, you could have used

Animator animator = handTransform.gameObject.GetComponentInParent<Animator>();

but if there were any animators hiding in the heirarchy, it might fidn that animator before finding the one in the root of the character. Best to pass along the Animator and be sure every time. (Plus, GetComponentInParent, like FindObjectOfType, is a pretty expensive operation),.

1 Like

Hey, thanks for the info!

I had wondered if GCIP would be expensive since it sounds like it’s searching the hierarchy to some degree, which seems to be the main cost factor between those functions. Is there a cheat sheet for which Unity functions are considered expensive?

There’s not really a cheat sheet out there that I know of. Much of it is learned through experience and thinking about what a particular function is supposed to do. Some editors (like Jetbrains Rider) will also point out expensive method invocations.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms