- Home /
audiosources not playing after object destruction
I have enemy spaceships in my game using the same script for their AI and that particular script also holds the sound effects played from those objects. I am using this technique in my previous question for the sounds. But I have a certain problem.
When the first spaceship gets destroyed in the scene the explosion sound stops playing when other spaceships explode. It gives "Can not play a disabled audio source" warning in the editor. But the wierd thing ONLY explosion sound stop playing other sounds play just fine like engine sound, weapon sound etc. Is there any way to reload the sound or something so the explosion sound plays fine for the rest of the spaceships that are alive?
Is the explosion sound attached to the spaceship itself? I would have thought that sound persisting after an object is destroyed would be a bigger bug! Try making the explosion sound separate from the rest of the system- similarly to how you would create an explosion particle effect that exists momentarily after the spaceship is destroyed.
I will explain in detail how this happens.
I have 5 npc spaceships in my scene. I have a NPCAI script that is attached to these objects. Also this script has the audioclips and audiosources for the npc spaceships. Since all npc spaceships use the same sounds I have the same sounds attached to every spaceship object via the NPCAI script.
Now,problem is this. When a NPC spaceship dies it plays the explosion sound just fine. But after that when another npc spaceship dies I get the warning "Can not play a disabled audio source" and I hear no sound. But this happens only for the explosion sound. NPC engine sounds, weapon sounds etc just plays fine without any warnings even after the first spaceship gets destroyed.
I can probably add a seperate child gameobject to the npc spaceships and play the explosion sound that way but I can see the other sounds play just fine so I dont understand why this happens to the explosion sound.
Could the explosion AudioSource reference be a static variable? Static variables are unique: if this reference is static, each new ship created will overwrite it, thus all ships end referencing the same AudioSource.
I will try that.
$$anonymous$$eanwhile I added an audiosource to the explosion itself so everytime the particle effect instantiates explosion sound plays fine that way. But now it has other problems, as multiple sounds dont play together sometimes but for now I solved the main issue :).
Thanks for the help.
Your answer
Follow this Question
Related Questions
Volume Control with Slider 1 Answer
Can I play multiple AudioSources from one gameobject? 8 Answers
How to play sound while animation load using assetbundle 0 Answers
How to control backsound on/off in the third scene 1 Answer
Play Sound On Object In Another Scene With Button from First Scene 1 Answer