Search Unity

Audio doesn't work

Discussion in 'Scripting' started by udix, Aug 3, 2017.

  1. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    Hello guys, I'm very new to Unity, been doing the Scavengers tutorial (2D Roguelike) from the learning section.
    I've reached the 13 out of 14 video, just adding the sound
    I'm doing exactly as they do it in there, but it does not work.
    I've been typing the exact same code, and did the exact same SoundManager game object, and it just doesn't work.
    I'm currently half way of the video tutorial to add the sound, but the background music should be on by now.

    here is the code of my SoundManager

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SoundManager : MonoBehaviour {
    7.  
    8.     public AudioSource efxSource;
    9.     public AudioSource musicSource;
    10.     public static SoundManager instance = null;
    11.  
    12.     public float lowPitchRange = .95f;
    13.     public float highPitchRange = 1.05f;
    14.  
    15.     void Awake ()
    16.     {
    17.         if (instance = null)
    18.             instance = this;
    19.         else if (instance != this)
    20.             Destroy(gameObject);
    21.  
    22.         DontDestroyOnLoad(gameObject);
    23.     }
    24.  
    25.     public void PlaySingle(AudioClip clip)
    26.     {
    27.         efxSource.clip = clip;
    28.         efxSource.Play();
    29.     }
    30.    
    31.     public void RandomizeSfx(params AudioClip [] clips)
    32.     {
    33.         int randomIndex = Random.Range(0, clips.Length);
    34.         float randomPitch = Random.Range(lowPitchRange, highPitchRange);
    35.  
    36.         efxSource.pitch = randomPitch;
    37.         efxSource.clip = clips[randomIndex];
    38.         efxSource.Play();
    39.     }
    40.  
    41. }
    42.  
    and a screen shot

     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,396
    Can you show the code that calls either PlaySingle or RandomizeSfx?
     
  3. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    Don't have that.
    I'm at the same stage as the tutorial, and when he launch the game, the sound works.
    the SoundManager is the only new thing that I added
    but if in the tutorial video it works for him, then it should work for me too.
     
  4. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    I've put the Audio Source in the SoundManager, and checked the "Loop" same as he did.
    here is the tutorial video, im at 6:13, this is where he launches the game, and the background music works
     
  5. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    What you have so far should work.. you've got your sound manager in the scene.. your sound manager has an audio source on it with a clip attached... it's set to play on awake. Weird.

    Open up the audio right in Unity and play it. You can click on the audio file in Unity's project explorer, and there should be a play button or something in the preview. Make sure you're hearing sounds. Maybe your PC's sound isn't working right..??
     
  6. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    I did open the audio file, and it does work.
    But in game, nothing.
    This makes me frustrated, I don't want to keep going until I got it all working
     
    Last edited: Aug 3, 2017
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,455
    Does your Camera have an AudioListener component attached to it? Did you somehow end up with multiple SoundManager script instances in the scene?
     
    cstooch likes this.
  8. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    How can I check if I have multiple SoundManager script instances?

    And yes, my Camera does have AudioListener component attached
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,455
    Add a log statement in your Start method or search for "t:SoundManager" in the scene hierarchy.
     
  10. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    Only one in the hierarchy
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,607
    Since you are on windows, you should have a sound icon in your system tray (I see it in your screenshot, so it's there). When you play the game, click on that icon and see if sound is playing there. If you don't see anything there, check the mixer to make sure the volume on the Unity player isn't muted/turned down.
     
  12. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    It was the first thing I did.
    Unity volume is on max.
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,607
    Is your SoundManager gameobject inactive?
     
  14. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    How can I check that?
    the V in the SoundManager is checked (in the inspector)
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,607
    No, it is, I see it on in the one screenshot now. Well, you could zip up your project and provide it here.

    Other thing to try is to try to play the audio through the script to see if that even works.

    I also thought it could be your sound was set up as 3d, but if you're following the tutorial, I don't think that would be it.
     
  16. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,607
    I can look at it later when I'm home, unless someone else gets a chance to.
     
  18. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    Thank you.
    Much appreciated.
     
  19. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    95
    Code (CSharp):
    1. if(instance = null)
    you have the above in your code but it should read

    Code (CSharp):
    1. if (instance == null)
    a double == means is it equal, not it equals, might help along the way

    It looks to me that your instance of the soundmanager class is never being set, so it just keeps getting nullified.
     
    cstooch and KelsoMRK like this.
  20. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,607
    Good catch! Surprised how many of us looked at this and didn't notice that. And fixing that will fix your sound playing issue.
     
  21. udix

    udix

    Joined:
    Aug 3, 2017
    Posts:
    11
    Omg thank you so much!
    And a really good catch, thank you again!
    The sound works!
    Now I can continue :)

    And if we're all already here, any tips for a Unity rookie ?
    would like articles and such to read, tutorials won't be enough to learn I guess
     
  22. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,607
    Honestly. Practice. Tutorials really are great. Unity has a lot of good examples and youtube has several good examples done by various people. A lot of times they might even mention tips and tricks to help out.

    Unity also publishes blog post and articles over different things every now and then.

    Also, many times if you just Google for something you can usually find other blogs and articles about the feature and how it may have been handled.

    Don't give up either! Even when you run into an error. Debugging is a part of programming, even if it does seem frustrating at times.