Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Help] Play audio before destroy?

Discussion in 'Scripting' started by Aprial, Sep 23, 2014.

  1. Aprial

    Aprial

    Joined:
    Dec 22, 2013
    Posts:
    28
    Hello,

    The char destroy before play the audio; need to play audio then destroy the char. please advice how to change the script.

    / C#
    bool hasPlayed = false;
    void OnCollisionEnter (Collision col)
    {
    if (collision.gameObject.name == "cube")
    if (!hasPlayed) {
    audio.PlayOneShot(clip);
    hasPlayed = true;
    Destroy (gameObject);
    }
    }
    }


    Thanks
    April
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Change "Destroy(gameObject)" to "Destroy(gameObject, clip.length)".
     
  3. Aprial

    Aprial

    Joined:
    Dec 22, 2013
    Posts:
    28
    Thank you Joe!
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Always happy to help!