Referencing AudioSource from a prefab script

Ok, so i know you cant drag objects from the hierarchy into a prefab in the inspector.
i have my sound effects on objects in the hierarchy, and i want the prefabs to use them as the game goes.

this code does work in start of the prefabs script:

enemyExplodesSound = FindObjectOfType<AudioSource>();

But i’m not sure if this is a good way of doing this, i read bad things about FindObjectOfType that its slow.

I tried GetComponent instead but that didnt work.

my audio objects are in an object called
—EnemySounds—
EnemyExplodes (This is where the audio source is)

so how do i reference that?

I dunno what all that complication is above there… put the AudioSource and the controlling script on the enemy, drag a reference into the script and be done with it.

When i destroy the enemy object the sound doesnt play

Find out why, use Debug.Log()

If it’s because he’s gone, do one of:

  • delay the Destroy()

  • deparent the sound, then destroy

  • use a central sound manager

  • something else?

ok thanks Kurt