Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Get the Audio time value ?

Discussion in 'Audio & Video' started by Magic-Goupil, Apr 7, 2022.

  1. Magic-Goupil

    Magic-Goupil

    Joined:
    Mar 19, 2020
    Posts:
    8
    Hello,
    I would like to recover the time of an audio, to make objects appear at the end of it.

    Example: If my audio lasts 10 seconds, I would like to do for example Audio.getTime() which would give me 10 seconds, and use this value in a Wait() which I will code.

    Audio.time() doesn't seem to work, or I'm doing it wrong.

    Another solution can be expected, the final goal is to start an action in the game (appearance of an object or event) at the end of an audio.

    I thank you in advance for your answers,
    Sincerely.
     
  2. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
  3. Magic-Goupil

    Magic-Goupil

    Joined:
    Mar 19, 2020
    Posts:
    8
    Thanks bro ! It's work :D

    For those who want to know, I post my code here, it allows to trigger a script after the end of an audio. In my case, I make an object disappea :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ActiveTP_BAF : MonoBehaviour
    6. {
    7.     public AudioSource audioSource;
    8.     private AudioClip audioClip;
    9.  
    10.     public GameObject disappear;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         audioSource = GetComponent<AudioSource>();
    15.         audioClip = audioSource.clip;
    16.  
    17.         // Debug.Log("Audio Clip Length : " + audioSource.clip.length); --> Work :)
    18.         // Debug.Log("Audio Clip Length : " + audioClip.length); --> Work :)
    19.         StartCoroutine(Wait());
    20.     }
    21.  
    22.     private IEnumerator Wait()
    23.     {
    24.         yield return new WaitForSeconds(audioClip.length);
    25.         disappear.SetActive(true);
    26.     }
    27. }
     
    Kayckbr likes this.