#On Trigger With Sound

1 messages · Page 1 of 1 (latest)

proven totem
#

Hi All, tring to code up sound playing when an OnTrigger Enter Occurs, I want it to play as I collect the object. The issue i have is the console say s it cannot play a disabled audio source. I can't see what I have done done wrong in my code if anyone has any idea I'd really appreciate it 🙂

proven totem
#

Update: I changed to code to look like this and attached the sound file to a gameobject then referenced that object in the inspector. It now will play on awake but when i untick and go through the collision nothing happens.

proven totem
#

Okay, i looked up another tutorial, I've changed my script again, I have tested the sound by adding another track, but still the clip doesn't play

hybrid bridge
#

Are you using the correct method? Is your collider set to IsTrigger?

proven totem
#

my collider is yes, the collection system is working perfectly alongside a timer script, it just wont play the sound.

#

I also did a debug in the if statement and that worked when i triggered the object

hybrid bridge
#

Is the audio source active? Did you assign it from the inspector?

proven totem
#

I believe so, but just to be sure here's a look

hybrid bridge
#

Does the object this script is attached to have an audio source? Bcuz you get the audio source component in Start . . .

proven totem
#

that audio source is attached to the object

hybrid bridge
#

When you play the game, pause it and check that tickSource is not null from the inspector . . .

#

Where is/did you place the log to check the trigger runs. I don't see it?

proven totem
#

ill check that now!

#

i think i got it

#

nope nevermind

#

hhaha

#

okay so i made a new game object called audio

#

and attached the sound file to that

#

then i dragged it onto my tick source

#

this works if i do it after i play the game, pause and then add the audio to that source

hybrid bridge
#

Oh, comment out the Destroy line and test if the sound works . . .

#

I just wanted to make sure the component wasn't null. If you play then pause and check the variable, it should be populated . . .

proven totem
#

Yeah if i remove the destroy it works

#

if i add it back no sound

hybrid bridge
#

Makes sense. When you destroy the object, all of its components are destroyed as well. The audio source gets destroyed and the sound stops playing (right when you play it), so it never runs . . .

proven totem
#

okay that makes so much sense

#

im trying to think of alternatives

#

really I just want the sound to play to showcase in my project i know how to use sound clips with on trigger events

#

how would i instantiate the destroy function after the play ?

#

I could try on triggerenter for the sound and on exit destroy 😮

#

im gonna try that

#

🙂

#

yay c#

#

..... that was premature

#

i added the tick source to the destroy function thinking it would play when the function is called

#

maybe this doesnt work or its my syntax but i dont understand why it wouldnt

#

it never reaches my second debug "Hello2"

#

Update: I'm 90% of the way there, the sound now plays on collision and stops after i exit , the remaining issue is that it stops immediately, meaning if the player is running to fast they wont hear the clip as the exit the collider to fast any ideas on how to resolve this ?