Search Unity

Play Random audioclip and then destroy

Discussion in 'Scripting' started by redghost, Jul 30, 2007.

  1. redghost

    redghost

    Joined:
    Mar 7, 2007
    Posts:
    82
    Hi there,

    I want to create a sphere, which OnTriggerEnter function is destroyed and plays a random audioclip from a group of 4 audio clips.

    the first plan:

    I created an empty GameObject (called BassF) with an audiosource and I attached the following script, which basically sets a random audioclip for the audiosource from a selection of audioclips.

    var frag : AudioClip[];



    function Awake () {

    audio.clip = frag[Random.Range(0, frag.length)];

    }


    then I ceate a sphere with an attached script, which plays the audio clip of BassF when the sphere is triggered:

    var frag : AudioClip[];



    function Awake () {

    audio.clip = frag[Random.Range(0, frag.length)];

    }


    this way it plays the sound onTrigger but the audioclip doesn't change. I know, it's a function Awake right now, but when I try the functionUpdate or FixedUpdate it doesn't even play the sound.

    the Plan B:

    is having the following script attached to the sphere:

    var frag : AudioClip[];

    function OnTriggerEnter() {

    audio.PlayOneShot(frag[Random.Range(0, frag.length)]);

    }


    which works very good, but I want the sphere to be destroyed when is triggered, or at least dissappear. When I put the Destroy(gameObject) afterwards it doesn't play the clip, cause it is... destroyed!!! :eek: The only thing that I could do is to tell it destroy the sphere when the clip ends... but then it's not destroyed when it's triggered, which is not what I'm looking for...

    ...any help anyone???
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
  3. redghost

    redghost

    Joined:
    Mar 7, 2007
    Posts:
    82
    yes... yes... sometimes the mind just stucks on complicated thoughts and avoids the obvious...

    I'm not that stupid all the time :D :oops: :oops: