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

Audio array problem

Discussion in 'Scripting' started by sjurick, Mar 2, 2014.

  1. sjurick

    sjurick

    Joined:
    Jan 2, 2013
    Posts:
    16
    I am having a hard time getting my targets in my game to play sounds. I have a Target.cs script which has a TargetHit() method that's called from my Player.cs script each time the player clicks on a target. In the Target.cs script I have defined an audio array which I assign two different audio clips in the Inspector. My target object is a prefab and has an Audio Source added to it, and the Target.cs script attached. The Target.cs script does work because it also handles destroying of the prefab which it does do.

    The only way I can get the audio to work with my current setup is to drag one of the two audio clips into the Audio Clip slot in the Audio Source, then tick the Play On Awake box. Then when I run the game, each time a target is hit, I only hear the sound set in the Audio Source and NOT from my array selection.

    So, can someone please look at my code and tell me if I'm way off in my design?

    Here is the Inspector of my Target Prefab:

    $Screen Shot 2014-03-02 at 10.21.27 AM.png

    Here is the Target.cs

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Target : MonoBehaviour {
    6.  
    7.     public AudioClip[] audioClips;
    8.  
    9.     void Start () { }
    10.  
    11.     void Update () { }
    12.    
    13.     public void TargetHit ()
    14.     {
    15.         Debug.Log("Made it to Target.cs script");
    16.         audio.PlayOneShot(audioClips[Random.Range(0,audioClips.Length)]);
    17.         Destroy(this.gameObject);
    18.         Debug.Log ("Target Destroyed!");
    19.     }
    20. }
    21.  
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    Well you destroy the object, just as you ask it to play audio. This is why you won't here any audio. Best to either put in an invoke then destroy or have an audio mng who is never destroyed, but triggered when needed,
     
    Last edited: Mar 2, 2014
  3. sjurick

    sjurick

    Joined:
    Jan 2, 2013
    Posts:
    16
    Fixed it.

    First, I deleted the Audio Source component from the Target prefab GO
    Second, I added an Audio Source component to the Player GO, leaving the Audio Clip field empty.
    Third, I made changes to the way the array is called in the Target.cs script shown below:

    Code (csharp):
    1.  
    2.     public AudioClip[] audioClips;
    3.  
    4.     void Start () { }
    5.  
    6.     void Update () { }
    7.    
    8.     public void TargetHit ()
    9.     {
    10.         int i = Random.Range(0, audioClips.Length);
    11.         AudioSource.PlayClipAtPoint(audioClips[i], transform.position);
    12.         Destroy(this.gameObject);
    13.     }
    14.