Search Unity

Audio source attached in inspector gets de-attached when restarting scene

Discussion in 'Audio & Video' started by Nikola310, May 14, 2016.

  1. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    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:

    Code (CSharp):
    1.     publicclassHurtEnemy:MonoBehaviour{
    2.     publicint damageToGive;
    3.     privateint currentDamage;
    4.     publicGameObject redDamageBurst;
    5.     publicGameObject blueDamageBurst;
    6.     publicGameObject purpleDamageBurst;
    7.     publicTransform hitPoint;
    8.     publicGameObject damageNumber;
    9.     privatePlayerStats pStats;
    10.     publicAudioSource slimeHurtSound;
    11.     publicAudioSource daggerGuyHurtSound;
    12.     voidStart()
    13.     {
    14.     pStats =FindObjectOfType<PlayerStats>();
    15.     }
    16.     voidUpdate()
    17.     {
    18.     }
    19.     voidOnTriggerEnter2D(Collider2D other)
    20.     {
    21.     if(other.gameObject.tag =="RedSlime")
    22.     {
    23.     //slimeHurtSound.Play ();
    24.     currentDamage = damageToGive + pStats.currentATK;
    25.     other.gameObject.GetComponent<EnemyHealth>().HurtEnemy(currentDamage);
    26.     Instantiate(redDamageBurst, hitPoint.position, hitPoint.rotation);
    27.     slimeHurtSound.Play();
    28.     var clone =(GameObject)Instantiate(damageNumber, hitPoint.position,Quaternion.Euler(Vector3.zero));
    29.     clone.GetComponent<FloatingNumbers>().damageNumber = currentDamage;
    30.     }
    31.     if(other.gameObject.tag =="BlueSlime")
    32.     {
    33.     //slimeHurtSound.Play ();
    34.     currentDamage = damageToGive + pStats.currentATK;
    35.     other.gameObject.GetComponent<EnemyHealth>().HurtEnemy(currentDamage);
    36.     Instantiate(blueDamageBurst, hitPoint.position, hitPoint.rotation);
    37.     slimeHurtSound.Play();
    38.     var clone =(GameObject)Instantiate(damageNumber, hitPoint.position,Quaternion.Euler(Vector3.zero));
    39.     clone.GetComponent<FloatingNumbers>().damageNumber = currentDamage;
    40.     }
    41.     if(other.gameObject.tag =="PurpleSlime")
    42.     {
    43.     //slimeHurtSound.Play ();
    44.     currentDamage = damageToGive + pStats.currentATK;
    45.     other.gameObject.GetComponent<EnemyHealth>().HurtEnemy(currentDamage);
    46.     Instantiate(purpleDamageBurst, hitPoint.position, hitPoint.rotation);
    47.     slimeHurtSound.Play();
    48.     var clone =(GameObject)Instantiate(damageNumber, hitPoint.position,Quaternion.Euler(Vector3.zero));
    49.     clone.GetComponent<FloatingNumbers>().damageNumber = currentDamage;
    50.     }
    51.     if(other.gameObject.tag =="DaggerGuy")
    52.     {
    53.     //daggerGuyHurtSound.Play ();
    54.     currentDamage = damageToGive + pStats.currentATK;
    55.     other.gameObject.GetComponent<EnemyHealth>().HurtEnemy(currentDamage);
    56.     Instantiate(redDamageBurst, hitPoint.position, hitPoint.rotation);
    57.     daggerGuyHurtSound.Play();
    58.     var clone =(GameObject)Instantiate(damageNumber, hitPoint.position,Quaternion.Euler(Vector3.zero));
    59.     clone.GetComponent<FloatingNumbers>().damageNumber = currentDamage;
    60.     }
    61.     }
    62.     }
    And yes, I am using UnityEngine.UI; and UnityEngine.Audio;