I’m working on a 2D RPG, and I’m experiencing a problem when restarting the scene.
When the player first spawns (when you load up the game), and attack the enemies they play their sounds when getting hurt, get hurt and can get killed. But, upon going into a house and going outside again, the audio source (SlimeHurtSound and DaggerGuyHurtSound) gets de-assigned in the inspector of the player’s sword swing (the sword swing does the damage, and plays a sound depending on the enemy type). Everything else gets done when attacking, but playing the sound doesn’t.
!
Here is the HurtEnemy script attached to the sword swing:
public class HurtEnemy : MonoBehaviour {
public int damageToGive;
private int currentDamage;
public GameObject redDamageBurst;
public GameObject blueDamageBurst;
public GameObject purpleDamageBurst;
public Transform hitPoint;
public GameObject damageNumber;
private PlayerStats pStats;
public AudioSource slimeHurtSound;
public AudioSource daggerGuyHurtSound;
void Start ()
{
pStats = FindObjectOfType<PlayerStats> ();
}
void Update ()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "RedSlime")
{
//slimeHurtSound.Play ();
currentDamage = damageToGive + pStats.currentATK;
other.gameObject.GetComponent<EnemyHealth> ().HurtEnemy(currentDamage);
Instantiate (redDamageBurst, hitPoint.position, hitPoint.rotation);
slimeHurtSound.Play ();
var clone = (GameObject) Instantiate(damageNumber, hitPoint.position, Quaternion.Euler (Vector3.zero));
clone.GetComponent<FloatingNumbers>().damageNumber = currentDamage;
}
if(other.gameObject.tag == "BlueSlime")
{
//slimeHurtSound.Play ();
currentDamage = damageToGive + pStats.currentATK;
other.gameObject.GetComponent<EnemyHealth> ().HurtEnemy(currentDamage);
Instantiate(blueDamageBurst, hitPoint.position, hitPoint.rotation);
slimeHurtSound.Play ();
var clone = (GameObject) Instantiate (damageNumber, hitPoint.position, Quaternion.Euler (Vector3.zero));
clone.GetComponent<FloatingNumbers> ().damageNumber = currentDamage;
}
if(other.gameObject.tag == "PurpleSlime")
{
//slimeHurtSound.Play ();
currentDamage = damageToGive + pStats.currentATK;
other.gameObject.GetComponent<EnemyHealth> ().HurtEnemy(currentDamage);
Instantiate(purpleDamageBurst, hitPoint.position, hitPoint.rotation);
slimeHurtSound.Play ();
var clone = (GameObject) Instantiate (damageNumber, hitPoint.position, Quaternion.Euler (Vector3.zero));
clone.GetComponent<FloatingNumbers> ().damageNumber = currentDamage;
}
if(other.gameObject.tag == "DaggerGuy")
{
//daggerGuyHurtSound.Play ();
currentDamage = damageToGive + pStats.currentATK;
other.gameObject.GetComponent<EnemyHealth> ().HurtEnemy(currentDamage);
Instantiate (redDamageBurst, hitPoint.position, hitPoint.rotation);
daggerGuyHurtSound.Play ();
var clone = (GameObject) Instantiate(damageNumber, hitPoint.position, Quaternion.Euler (Vector3.zero));
clone.GetComponent<FloatingNumbers>().damageNumber = currentDamage;
}
}
}
And yes, I am using UnityEngine.UI; and UnityEngine.Audio;