I’ve been putting together a simple game, and I want to play a sound effect for when an enemy is destroyed. The enemy, which is a prefab (not sure if that affects anything), is destroyed when the player clicks on it. At the moment, the sound effect is played when the enemy prefab is generated, not when it is destroyed. I’m very new at this, so if anyone is willing to help out, I would greatly appreciate any assistance. Thanks in advance.
using UnityEngine;
using System.Collections;
public class EnemyDestroy : MonoBehaviour {
public int scoreValue = 1;
public AudioClip theSound;
AudioSource soundClip;
void Start(){
soundClip = GetComponent<AudioSource> ();
soundClip.playOnAwake = false;
}
// Use this for initialization
void OnMouseDown(){
if (gameObject.tag == "Enemy") {
ScoreManager.score += scoreValue;
soundClip.PlayOneShot (theSound, 0.8f);
Destroy (gameObject);
}
}
}
I should have seen this earlier... There's a pair of braces missing. if (Input.GetButton("Fire1") && Time.time > NextFire) { NextFire = Time.time + FireRate; Instantiate(Shot, ShotSpawn.position, ShotSpawn.rotation); }
– doublemax