There is such a script:

using UnityEngine; public class Audiou2 : MonoBehaviour { public AudioClip Audi; public AnimationClip a; public GameObject As; void OnGUI() { if(GUI.Button(new Rect(15,105,81,81),"")) { audio.clip = Audi; audio.Play(); } if(GUI.Button(new Rect(15,105,81,81),"")) { As.animation.Play(a.name); } } } 

It is necessary that when you click on the button that is displayed on the screen, the script starts simultaneously playing the audio file and turning on the animation. How is it possible to implement these script actions?

    1 answer 1

    Or so

     void OnGUI() { if(GUI.Button(new Rect(15,105,81,81),"")) { audio.clip = Audi; audio.Play(); As.animation.Play(a.name); } } 

    Or go to the UI. Do public method

     public void Activation(){ audio.clip = Audi; audio.Play(); As.animation.Play(a.name); } 

    and in the inspector throw this script to the right button and select the Activation method

    • The script includes either an animation or an audio file (which you write first in the script), but not all at once. - Kokoc2634
    • @ Kokoc2634 can you just animate sooner / later than the time when the sound should occur? Then the audio just put the desired delay and all. But they have to play almost at the same time ....... try to make PlayOneShot instead of Play - Alexey Shimansky
    • Thanks, it helped. One more question. The script starts the animation when you click on the button. If you press again (immediately) on the button, the animation will not start over, but the one that has not ended will continue. Is it possible to force the animation to play first when a button is pressed? For example. There is an animation that catches up with the object. It is necessary to make sure that when you click on the button, the animation is played first (at the first point) and you do not have to wait until it finishes (reaches the last point) so that it can be started again. - Kokoc2634
    • to do this, first check whether the animation is playing, if so, then stop and start again if (animation.isPlaying) {animation.Stop; animation.Play (a.name); } if Stop did not help (which is strange), then you have to turn off the animator and turn it on again - Art
    • @ Kokoc2634 depends on what kind of animation you use .... if Animator - that is, the overloaded public void Play(string stateName, int layer = -1, float normalizedTime = float.NegativeInfinity); method public void Play(string stateName, int layer = -1, float normalizedTime = float.NegativeInfinity); that is, it literally says play such and such an animation, in such and such a layer and from such and such a moment ....... that is, for example, you can write anim.Play("My Animation", -1, 0f); and everything ... and in 3d in Animation there’s probably something like that .... docs.unity3d.com/ScriptReference/Animation.Play.html public bool Play(string animation, PlayMode mode = PlayMode.StopSameLayer); just - Alexey Shimansky