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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with respawning and Game Over sounds

Discussion in 'Scripting' started by JosephB2000, May 23, 2015.

  1. JosephB2000

    JosephB2000

    Joined:
    May 18, 2015
    Posts:
    8
    I am making a simple script which makes the player respawn when he falls. The actual respawning part works, but I have tried to add a 'Game Over' sound that plays straight after respawning. Everytime I respawn, the sound does play, but it also plays straight after starting the game up.

    Here's the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BallHealth : MonoBehaviour {
    5.    
    6.     public bool isRestarting = false;
    7.     public float maxFallDistance = -10f;
    8.     public AudioClip GameOver;
    9.  
    10.     void Update ()
    11.     {
    12.         if (transform.position.y < maxFallDistance)
    13.         {
    14.             if(isRestarting == true)
    15.             {
    16.                 AudioSource audio = GetComponent<AudioSource>();
    17.                 audio.Play ();
    18.             }
    19.             isRestarting = true;
    20.             Application.LoadLevel("Level01");
    21.         }
    22.     }
    23. }
    As I said, the sound does play when I respawn, but it also plays when I start the scene up. Any ideas why?
     
  2. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    That's because on your Audio Source component you have "Play on Awake" checked. Disable that and it will be fine.

     
  3. JosephB2000

    JosephB2000

    Joined:
    May 18, 2015
    Posts:
    8
    Well, now the sound doesn't play when I start up the scene, but neither does it play when I respawn, as if it was completely disabled...

    All I changed was the 'Play On Awake'...
     
  4. Zenov

    Zenov

    Joined:
    Jul 28, 2014
    Posts:
    53
    hmm odd... another option could be to just stop the audio to begin with and leave it checked
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BallHealth : MonoBehaviour {
    6.  
    7.     public bool isRestarting = false;
    8.     public float maxFallDistance = -10f;
    9.     public AudioClip GameOver;
    10.  
    11.     private AudioSource audio;
    12.  
    13.     void Start()
    14.     {
    15.         audio = GetComponent<AudioSource>();
    16.         audio.Stop();
    17.     }
    18.  
    19.     void Update ()
    20.     {
    21.         if (transform.position.y < maxFallDistance)
    22.         {
    23.             if(isRestarting == true)
    24.             {
    25.                 audio.clip = GameOver;
    26.                 audio.Play ();
    27.                 isRestarting = false;
    28.                 Application.LoadLevel("Level01");
    29.             }
    30.         }
    31.     }
    32. }
    33.  
     
    Last edited: May 23, 2015
  5. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    That's because when you respawn you don't call anything, you just spawn the gameobject. On your Start() you should call Play() on the AudioSource component.
     
  6. JosephB2000

    JosephB2000

    Joined:
    May 18, 2015
    Posts:
    8
    Could it be that because I'm loading the whole level, the script is also reloaded?
    This would mean that the variable isRestarting is always false...

    I'm not that sure though.
     
  7. JosephB2000

    JosephB2000

    Joined:
    May 18, 2015
    Posts:
    8
    Fixed it:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BallHealth : MonoBehaviour {
    5.  
    6.     public float maxFallDistance = -10f;
    7.  
    8.     void Update ()
    9.     {
    10.         if (transform.position.y < maxFallDistance)
    11.         {
    12.             GetComponent<AudioSource>().Play();
    13.             transform.position = new Vector3(0, 1, 0);
    14.         }
    15.     }
    16. }
    The 'new Vector3' on line 13 is the starting position of the player.
     
  8. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Indeed. The script is getting reloaded.

    You can avoid that by using :

    Code (CSharp):
    1. private void Start()
    2. {
    3.      DontDestroyOnLoad(gameObject);
    4. }
    This will allow your gameObject to stay in the game even when you load another scene.
     
  9. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136

    It's not cool to use GetComponent in Update(), as it is a slow method.

    You should change it like that :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BallHealth : MonoBehaviour {
    5.  
    6.     public float maxFallDistance = -10f;
    7.  
    8.     private AudioSource audioSource;
    9.  
    10.     void Start ()
    11.     {
    12.         audioSource = GetComponent<AudioSource>();
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.         if (transform.position.y < maxFallDistance)
    18.         {
    19.             audioSource.Play();
    20.             transform.position = new Vector3(0, 1, 0);
    21.         }
    22.     }
    23. }