Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I can't get this play.oneshot to work properly.

Discussion in 'Scripting' started by LarryWells, Jul 31, 2016.

  1. LarryWells

    LarryWells

    Joined:
    Jun 1, 2016
    Posts:
    30
    I'm going to link my code so far but I just started last month so if it is hard to read and sloppy I'm sorry ahead of time. I basically want to have an audio file play when the GameObject is removed aka dies.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Death : MonoBehaviour {
    7.     public int SceneId = 0;
    8.     public Text loseText;
    9.     private bool isDead = false;
    10.     private bool enemyCollision = false;
    11.     public AudioClip deathSound;
    12.     AudioSource myAudio;
    13.  
    14.     void Start () {
    15.         myAudio = GetComponent<AudioSource>();
    16.         loseText.text = "";
    17.     }
    18.     void Update () {
    19.         if (this.transform.position.y < -6) {
    20.             Destroy (this.gameObject);
    21.             isDead = true;
    22.         } else if (enemyCollision == true) {
    23.             Destroy (this.gameObject);
    24.             isDead = true;
    25.         }
    26.         if (isDead == true) {
    27.            
    28.             loseText.text = "You died";
    29.             SceneManager.LoadScene (SceneId);
    30.  
    31.  
    32.         }
    33.     }
    34.  
    35.    
    36.         void OnTriggerEnter (Collider other) {
    37.         if (other.gameObject.name == "EnemyHitbox") {
    38.             enemyCollision = true;
    39.             myAudio.PlayOneShot (deathSound);
    40.         }
    41.  
    42.    
    43.     }
    44. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Are you destroying the object and then trying to tell it to play an audio? Or, appears you're trying to play an sound while destroying the object...and then loading a scene rather quickly.

    What it looks like is something is triggering the death, it sets a bool to true which signals the update to say it's dead, but then right after that you're telling it to load a scene.
     
  3. LarryWells

    LarryWells

    Joined:
    Jun 1, 2016
    Posts:
    30
    So I need a delay in between death and loading a scene? I'll try that.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Right now, that is just my theory. either your level is loading to quickly or your guy is dying before the sound actually plays, thus destroying the audiosource.

    To test, I would not destroy the enemy or load another scene and see if the audio plays.
     
  5. LarryWells

    LarryWells

    Joined:
    Jun 1, 2016
    Posts:
    30
    Oh ok, the problem was that the destroy was being called before the sound. I tried rearranging and rewriting things but I couldn't get the sound to play first so I just made a script on my camera and told it to play the sound whenever the player was destroyed (died).