I'm learning Unity.

I have two functions, one that transitions to a new scene and the other one is the button animation.

I add both functions to the onclick of a given button. I assume it calls both functions at the same time and executes the transition without running the animation.

I want the button to run the animation first (I'm using DOTWEEN) and once it's done to execute the transition.

  • \$\begingroup\$ I don't use Unity so can't say for certain but can you get the button to call the transition instead of OnClick? \$\endgroup\$ – Stephen Sep 27 '17 at 12:40

Unity generally does not execute "two funtions at the same time", because the game logic is single-threaded. What likely happens is that the function which starts the button animation is executed. The actual animation happens in a coroutine which would be processed throughout future updates.

Then the scene change function is executed. The scene change also interrupts the tweening coroutine, which means it is never finished.

What you actually want to do is start the tween coroutine and then do the scene change when the tween coroutine finished. The Unity UI system doesn't know about the tween you are doing, so it can't know when it will be finished. That unfortunately means that you can't cleanly separate these two functionalities (animation and scene-change) because the second depends on the first being completed.

To do something when a DOTWEEN tween is finished, pass it as a callback method to the OnComplete method of your Tweener. More about this in the DOTWEEN documentation under "Chained Callbacks".

  • \$\begingroup\$ Well he could use a coroutine, start the animation, yield for the duration of the animation and then do the scene change. \$\endgroup\$ – ElDuderino Sep 27 '17 at 13:30
  • \$\begingroup\$ @ElDuderino That would be exactly the same thing DOTWEEN does under the hood. \$\endgroup\$ – Philipp Sep 27 '17 at 13:32
  • \$\begingroup\$ My bad, I didn't know DOTWEEN. \$\endgroup\$ – ElDuderino Sep 27 '17 at 13:36

You can simply animate button using Button Transition.by default, unity is selected with Color Tint, change it from Color Tint to Animation and then you can use OnClick to translate new scene.

Unity Button animation

you can do simple animation for the button in Unity animation

Using Unity animation is better than DoTween and it's easy for small animations.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.