Animation Clip interfering with script?

Hi all,

I’ve struck a bit of a seemingly random issue.

I have some code that will reduce the pitch of an AudioSource as a GameObject moves from the top to the bottom of the screen and back up again (pitch down, pitch up etc).

There is also an Animator and Animation Clip in use. The clip just cycles the colour of the sprite to give the impression of it flashing. The animator is in a state of “abducting” when this happens.

All works well and as expected.

Today I decided to change a separate Animation Clip which is used for a separate state in the same Animator. I added a reference to the AudioSource and again made a couple of tweaks to the pitch. This was so that it would be changed as certain points during the animation. This is a separate state and separate clip to the one described above which cycles the colour - my point here being that they are utterly separate, do not execute at the same time, the only thing they have in common is the Animator.

When I ran the game, the code which made the pitch changes (pitch down/pitch up) seemed to no longer work. Having investigated, it was working, but it would seem that the values were getting overridden.

The moment I removed the changes to the other Animation Clip, which had the references to the AudioSource the code behaved as it should again.

I’m a little bit confused by this, as the Animator doesn’t go into the state where the clip references to the AudioSource under this condition, yet the references to the AudioSource seem to cause problems.

I was reluctantly adding these pitch changes to the Animation Clip in the first place, but it seemed like a quicker way to achieve the end result I was after. This feels like a bug, but perhaps I’m missing something.

In the second Animation Clip I refer to here, the pitch is set at 1 and increased just before the end of the sprite changes. It’s by belief that this is overriding the changes I’m trying to make in code - yet these two events should not be executing at the same time anyway, and certainly are no when you watch the Animator.

Has anyone had any similar issues? I’m currently running 2018.3.7

Just ran another quick test before posting this, I added the AudioSource property back to the Animation Clip, associated with the state that isn’t executed and again, it seemingly prevents the code from running, e.g. I don’t here the pitch changes being executed via the code, thus, reproducible.

Just wondered if anyone might have any thoughts on the above? I can provide some screenshots and any further details if the explanation above isn’t quite hitting the spot.

Thanks for your time :slight_smile:

This is a well-known issue, where the Animator writes to every single value any clip touches, even if no clips on the current state doesn’t animate that value.

You can turn that off on a state-by-state basis - the toggle is named “Write Defaults”, and is visible in the inspector when you select a state in the animator window. If you turn that off on a state, that state won’t write to values it’s not supposed to touch.

1 Like

Hi Baste,

Thank you so much, I doubt I would have ever stumbled across this had I not asked. I don’t like bumping my posts, but in this case I am really glad I did and that you took the time to reply, thank you.

Off to make some changes and re-implement my functionality! Have a great day! :slight_smile:

Hmmm, what you described makes real sense, although I have just removed this setting from the 3 states in the animator in question, one at a time, to test, and yet even with all three unchecked, the issue still occurs.

The clip which changes the audio pitch is playing as expected, but when the code executes, which changes the pitch for a different state, the pitch seemingly remains the same. Hmm…

Further Update

I tried closing an re-opening Unity, just on the off-chance it made any difference, it did, but not a positive one :slight_smile:

After I had re-opened the project and ran the game, this time when the game was in the state in which the “code” would be changing the pitch, there was no audio at all, e.g. the volume was 0. This would align with the properties not being written back as in the other state the volume is set to zero at the end of the animation clip.

I tried then adding the volume property to the other state and set it as one, ran the game and the audio was back, but the pitch still remains un-changed.

It looks like, at the moment at least, I’m going to have to either put all of the pitch changes in the animation, or, make them all via code, it doesn’t seem like Unity will currently support the mix.

I came across a logged issue on the Issue Tracker for the same problem but relating to transforms, sadly that’s been there several years so I’m not overly confident of a resolution any time soon.

In between now and my original post I also took the time to read through the release notes for the different versions between what I had and the latest, there were a few changes to animation but nothing which seemed to touch on the problem. I am currently running version 2019.1.9 but unsuccessfully.

Pitch changes via code isn’t going to be fun :frowning: