- Home /
Unity 3D Audio Clip Disabled
Hello,
I am attempting to make a sound clip play when I destroy an enemy in a space shooter type game. However it keeps giving me the following message: Can not play a disabled audio source UnityEngine.AudioSource:PlayOneShot(AudioClip) EnemyControllerBasic:OnTriggerEnter(Collider) (at Assets/Stewart_Tafari_Lab2/Scripts/EnemyControllerBasic.cs:36).
Any idea why it is doing this?
using UnityEngine; using System.Collections;
public class EnemyControllerBasic : MonoBehaviour {
// The game object which spawns us.
private ShipPlayerController PlayerShipCtrl;
// The game object which spawns the projectile.
private Mover Bolt;
// The different IDs for the AI states
enum AIMode { Normal, Ramming, SteerTowards, Charge };
// The variable which holds the current AI state
private AIMode CurrentAIState;
// Holds the audio clip to play when
// this object is destroyed
[SerializeField]AudioClip blowupSfxClip;
// List of pickup TYPES to spawn
[SerializeField]GameObject[] PickupTypes;
// Used to control how fast the game object moves
[SerializeField]float MoveSpeed = 4.0f;
// The speed to use when in "ramming" mode
[SerializeField]float RamSpeed = 5.0f;
// The speed to use when in "charge" mode
[SerializeField]float ChargeSpeed = 6.0f;
// Called by the engine when this object collides
// with another object containing collision
void OnTriggerEnter (Collider other) {
// Hit the player's projectile?
if (other.tag == "PlayerProjectile") {
// A projectile= instant kills us
Destroy(gameObject);
// play the blowup audio sfx file
audio.PlayOneShot(blowupSfxClip);
// Drop a pickup
SpawnPickup ();
}
// Hit the player's ship?
else if (other.tag == "PlayerShip") {
// Wrecking with the player kills us
Destroy(gameObject);
// Remove points from the player for hitting the enemy
PlayerShipCtrl.ModScore (-1);
// Remove shield from the player for hitting the enemy
PlayerShipCtrl.ModShield (-1);
// Remove health from the player for hitting the enemy
PlayerShipCtrl.ModHealth(-1);
}
}
Answer by ahmedbenlakhdhar · Dec 15, 2014 at 01:42 AM
Here:
Destroy(gameObject); //You destroy the game object
audio.PlayOneShot(blowupSfxClip);//You use its audio source component after its destruction
You cannot use the audio
attribute of a destroyed game object.
Try to play the audio in another audio source of an enduring game object.
Your answer
Follow this Question
Related Questions
Make player not be seen by AI, when player in foilage and shadows. 1 Answer
AI enemy scripting help 1 Answer
How do I make ai play a certain sound, and then make the other ai reply? 0 Answers
Shooting Damage Help 1 Answer
Stay Back! 2 Answers