Search Unity

Triggering different audio clips by name in script?

Discussion in 'Editor & General Support' started by Morgan, Jun 16, 2006.

  1. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    What's the best way to make an object play different collision sounds based on scripted logic? (Like having a "scarier" collision sound when the player's health is low, or randomly choosing from multiple clips.)

    I tried using audio.clip = "Sound Name" (in OnCollisionEnter just before the audio.Play) in the hopes that it would override the chosen sound I originally dragged onto the object. But that just generates an error.

    Thanks for any tips!
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You need to assign a reference to an object.

    Code (csharp):
    1.  
    2. var clip : AudioClip;
    3.  
    4. audio.clip = clip;
    5.  
     
  3. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Thanks a lot--once I get away from my habits from OTHER tools I'll be better off!
     
  4. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Follow-up: it is possible to generate an object reference from strings?

    In other words, take three variables that equal "angry," "emu," and the number 2, and achieve audio.clip = angryemu2; from that?

    (So that your script could pick an emotion, an animal, and a number and choose a sound without you having to key in all the possibilities? I assume you'd still have to declare all the vars and drag every sound into the Inspector, though.)

    Just curious for future reference.
     
  5. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    I bet there's a simple concatenation function in one of the .Net classes. I'm newer than you at this programming stuff, though, so don't quote me!
     
  6. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    I don't think this is possible in .Net, at least without some fancy reflection stuff. However, I am not a .Net expert. It seems to primarily be built around Static typing so it is not straight forward. Ah, the beauty of true dynamic typing :)

    However, you could probably build your string then use GameObject.Find("yourBuiltString") to get the game object with the audio clip, then use the GetComonent function to get the clip to assign. While not as straight forward it should work. If I remember correctly, you have to have any assets you want to use assigned to a game object otherwise Unity will not add the asset to the executable, or something like that.
     
  7. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    I see. I'll keep that possibility in mind.

    More audio Qs:

    1. Is there a way to make my object emit more than one sound at a time? Each sound now cuts off the previous one.

    2. How can my script access the audio sources of the object's children? What kind of var declaration would I use?