Search Unity

Playing sound effects from game object

Discussion in 'Scripting' started by vit_the, Feb 20, 2018.

  1. vit_the

    vit_the

    Joined:
    Feb 3, 2018
    Posts:
    13
    Hello!

    Help no longer required for this issue. For details, scroll to the bottom of this post and see UPDATE 1

    I appreciate the great help here.

    My challenge here is to bring in an easter egg object which when triggered will do some stuff to other objects in the game and play a sound.

    So far I can find the other objects in the scene and do stuff to them visually with tweening. Its fun.

    However I also want to play some sound effects to coincide with the animation provided by the tweening. I am using a coroutine to decide whether the triggers are activate properly, and to find, trigger sound effects and tween the other game objects.

    I am trying to do this by using the solution given by Unity support for playing multiple audio from one source:

    https://support.unity3d.com/hc/en-u...y-multiple-Audio-Sources-from-one-GameObject-

    And I get compiler errors
    Code (CSharp):
    1.  
    2. Assets/Scripts/Easter Eggs/Ricks Spooky Cube/RicksSpookyCube.cs(28,17): error CS0029: Cannot implicitly convert type `UnityEngine.AudioSource[]' to `UnityEngine.AudioSource'
    3. Assets/Scripts/Easter Eggs/Ricks Spooky Cube/RicksSpookyCube.cs(61,23): error CS0117: `UnityEngine.AudioClip' does not contain a definition for `PlayOneShot'
    4. Assets/Scripts/Easter Eggs/Ricks Spooky Cube/RicksSpookyCube.cs(65,23): error CS0117: `UnityEngine.AudioClip' does not contain a definition for `PlayOneShot'
    5. Assets/Scripts/Easter Eggs/Ricks Spooky Cube/RicksSpookyCube.cs(68,23): error CS0117: `UnityEngine.AudioClip' does not contain a definition for `PlayOneShot'
    6.  
    Here are the relevant parts of the code vis-a-vis the Unity support solution. Each AudioClip.PlayOneShot is triggered inside of a Coroutine, incase that is relevant.

    Code (CSharp):
    1.  
    2.     // SFX handling
    3.     public AudioClip ricksCatchphrase;
    4.     public AudioClip rocketStageOne;
    5.     public AudioClip rocketStageTwo;
    6.     public AudioSource audio;
    7.  
    8.     private void Start()
    9.     {
    10.         audio = GetComponents<AudioSource>();
    11.     }
    12.  
    13. IEnumerator coroutineA() {
    14. ...
    15.             AudioClip.PlayOneShot(rocketStageOne, 1f);
    16.             Debug.Log("WE'RE REALLY DOING IT NOW!");
    17.             yield return new WaitForSeconds(2f);
    18.             AudioClip.PlayOneShot(ricksCatchphrase, 1f);
    19.             AudioClip.PlayOneShot(rocketStageTwo, 1f);
    20. ...}
    21.  
    I have attached this script and hooked up the AudioClip fields to their respective soundfiles. I have also attached an unaltered Audio Source component.

    I tried many different things such as attaching 3 blank audiosource, and also filling the audiosource with the desired clips, I have also tried attempting to use
    Code (CSharp):
    1.  
    2. audio.PlayOneShot(rocketStageOne, 1f);
    3.  
    and have not had any luck resolving the 1st error about 'Cannot implicity convert type' no matter the configuration of audiosources. I don't understand why it finds its error there when that seems like the most straightforward part I took from Unity support.

    Please advise.

    UPDATE 1
    The premise of this post was that Unity support's answer worked and I thought I was running into trouble integrating it with a Coroutine. However it looks like Unity Support's answer just plain doesn't work in Unity 2017.3.0f3.
    I took the code verbatim as in the presentation and get the same errors.

    So the scenario is.. I have a GameObject doing some activity and for this object specifically I want to bring some sounds and play them by triggering them in a coroutine. How do I use sounds in Unity?

    I will look at a few tutorials for calling sounds from a script.
     
    Last edited: Feb 20, 2018
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Code (csharp):
    1. audio = GetComponent<AudioSource>();
    You have an extra 's' in there indicating you are expecting more than one component, but you do not define your variable as an array, which is why you get compiler errors.
    Code (csharp):
    1. audio.PlayOneShot(rocketStageOne, 1f);
     
  3. vit_the

    vit_the

    Joined:
    Feb 3, 2018
    Posts:
    13
    Thank you for the information, I will put this together as you advise and return to report how it goes.
     
  4. vit_the

    vit_the

    Joined:
    Feb 3, 2018
    Posts:
    13
    The solution worked perfectly, thanks a million!