Search Unity

Space Shooter - Playing multiple audio clips.

Discussion in 'Scripting' started by DreamingInsanity, Nov 12, 2017.

  1. DreamingInsanity

    DreamingInsanity

    Joined:
    Nov 5, 2017
    Posts:
    101
    Hello,

    This is one I have been struggling on. I want to play a background clip for my space shooter; which is very easy. But now comes the hard part - I want to play a secondary (maybe even a third) clip when something happens. I have looked around a lot and have found no help. There was one video but it used "AudioManager" which doesn't seem to exist anymore. Here is the code for the Game Controller script (that might not be the right script)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameController : MonoBehaviour
    5. {
    6.     public GameObject[] hazards;
    7.     public Vector3 spawnValues;
    8.     public int hazardCount;
    9.     public float spawnWait;
    10.     public float startWait;
    11.     public float waveWait;
    12.  
    13.     public GUIText scoreText;
    14.     public GUIText restartText;
    15.     public GUIText gameOverText;
    16.  
    17.     private bool gameOver;
    18.     private bool restart;
    19.     private int score;
    20.  
    21.     private Transform playerTransform;
    22.     private Transform Enemy;
    23.  
    24.     void Start ()
    25.     {
    26.         gameOver = false;
    27.         restart = false;
    28.         restartText.text = "";
    29.         gameOverText.text = "";
    30.         score = 0;
    31.         UpdateScore ();
    32.         StartCoroutine (SpawnWaves ());
    33.     }
    34.  
    35.     void Update ()
    36.     {
    37.         if (restart)
    38.         {
    39.             if (Input.GetKeyDown (KeyCode.R))
    40.             {
    41.                 Application.LoadLevel (Application.loadedLevel);
    42.             }
    43.         }
    44.     }
    45.  
    46.     IEnumerator SpawnWaves()
    47.     {
    48.         yield return new WaitForSeconds (startWait);
    49.         while (true)
    50.         {
    51.             for (int i = 0; i < hazardCount; i++)
    52.             {
    53.                 GameObject hazard = hazards[Random.Range (0, hazards.Length)];
    54.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    55.                 Quaternion spawnRotation = Quaternion.identity;
    56.                 Instantiate (hazard, spawnPosition, spawnRotation);
    57.                 yield return new WaitForSeconds (spawnWait);
    58.             }
    59.             yield return new WaitForSeconds (waveWait);
    60.  
    61.             if (gameOver)
    62.             {
    63.                 restartText.text = "Press 'R' for Restart";
    64.                 restart = true;
    65.                 break;
    66.             }
    67.         }
    68.     }
    69.  
    70.     public void AddScore (int newScoreValue)
    71.     {
    72.         score += newScoreValue;
    73.         UpdateScore ();
    74.     }
    75.  
    76.     void UpdateScore ()
    77.     {
    78.         scoreText.text = "Score: " + score;
    79.     }
    80.  
    81.     public void GameOver ()
    82.     {
    83.         gameOverText.text = "Game Over!";
    84.         gameOver = true;
    85.     }
    86. }
    Help would be appreciated,
    thanks, Dream.
     
  2. BillJackson

    BillJackson

    Joined:
    Dec 13, 2016
    Posts:
    6
    Add another AudioSource to your game. Assign your sound file and uncheck "Play on Awake." Then in the script where the thing happens, reference that AudioSource. Like:

    Code (CSharp):
    1. public AudioSource thingHappensAudio;
    That way you can drag it in in the inspector. Then when you want to play it, simply call the Play() method on it.

    Code (CSharp):
    1. thingHappensAudio.Play();
     
    Last edited: Nov 13, 2017
  3. DreamingInsanity

    DreamingInsanity

    Joined:
    Nov 5, 2017
    Posts:
    101
    Oh well, that was easy! Thanks!