Audio source attached to an enemy in inspector gets de-attached when restarting scene

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;

Hi. This seems to be a beginner problem.

I assume the house and outdoor are different scenes since you wrote “restarting scene”?

void Start ()
{
    pStats = FindObjectOfType<PlayerStats> ();
}

The problem seems to be that the audiosources are dragged into the reference slot before starting, but not assigned to the variables, when re-entering the outdoor scene therefore giving a MissingReference Exception.

The object containing these components is destroyed when changing to the house scene, and when the code is triggered to play a sound on them, it says “oops where is it now? Can’t play the sound”.

I’d suggest you do a GameObject.Find() in the start function I highlighted, so everytime you switch to the outdoor scene, the script find the references automatically.

Let me know if this helps, if not, feel free to tell me what happens. :wink:

I have fixed the bug by deleting the Canvas from all other scenes, but just keeping it in the first “starting” scene. Then, I attached a script that says “DontDestroyOnLoad(this)” and attached it to the Canvas in the “starting” scene.