Play and wait for audio to finish playing

前端 未结 2 1138
广开言路
广开言路 2021-01-22 07:52

in my project I want a sound to play and then for an objects active state to be set to false, at the moment both are happening at the same time so the sound doesn\'t play. If I

2条回答
  •  醉酒成梦
    2021-01-22 08:36

    Use coroutine as Joe Said. Start coroutine each time the collided object is enabled.

    void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Pick Up") 
         {
             if (!isPlayed) {
                 source.Play ();
                 isPlayed = true;
             }
         }
         if (other.gameObject.CompareTag ("Pick Up"))
         {
             other.gameObject.SetActive (true);
             count = count + 1;
             SetCountText ();
             StartCoroutine(waitForSound(other)); //Start Coroutine
         }
     }
    
    
    
     IEnumerator waitForSound(Collider other)
        {
            //Wait Until Sound has finished playing
            while (source.isPlaying)
            {
                yield return null;
            }
    
           //Auidio has finished playing, disable GameObject
            other.gameObject.SetActive(false);
        }
    

提交回复
热议问题