Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Play Audio and destroy trigger c#

Discussion in 'Scripting' started by bumbummen99, Jun 12, 2014.

  1. bumbummen99

    bumbummen99

    Joined:
    May 19, 2013
    Posts:
    10
    Hey guys,
    im working on a gameproject where sound plays a bigger role than graphics so i need a smart audio script.

    I want to place a invisible Cube in scene with a Mesh Collider (trigger).
    When player enters the mesh, it should play a defined sound and after the sound has played, the script destroys the cube with the script.

    I have played around on a testground a little bit but but i dont it working.
    Done that it destroys the trigger but then the audio stops playing.

    Can anyone please provide a script that handles this?


    Also it shouldnt work with a global variable because i need that very very often so it should be smart^^.

    here is what i got:
    Code (csharp):
    1.  
    2.   using UnityEngine;
    3.   using System.Collections;
    4.  
    5.   public AudioClip Sound;
    6.  
    7.   public class DestroyTrigger : MonoBehaviour {
    8.      void OnCollisonEnter(Collision coll)
    9.      {
    10.        if (coll.gameObject.tag == "Player"){
    11.          audio.PlayOneShot (Sound, 1.0f);
    12.        }
    13.  
    14.   }
    15.   }
    16.  
    i dont know how to do that audio goes on playing while the gameobject is away. please help :)

    MfG


    PS: Sorry for posting here but the tag system on unity answers is comletely buggy.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  3. bumbummen99

    bumbummen99

    Joined:
    May 19, 2013
    Posts:
    10
    thats to high for me :3 im new to c# ^^ but ill try thanks
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    or you could just destroy it in OnCollisionExit()...
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    That would destroy the audio also. PlayClipAtPoint will simply play a clip at a point in space then destroy its self. No need to keep the current object around.
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    I like how you defined the sound variable outside of the class xD

    Make sure that the collider is set to trigger

    Anyway, sorry that I couldn't answer this question earlier!
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyTrigger : MonoBehaviour {
    5.  
    6.     public AudioClip Sound;
    7.  
    8.     void OnTriggerEnter(Collider other) {
    9.         if (other.collider.gameObject.tag == "Player") {
    10.             audio.PlayOneShot(Sound, 1.0f);
    11.             Destroy(collider);
    12.             Destroy(gameObject, Sound.length);
    13.         }
    14.     }
    15. }