Search Unity

PlayClipAtPoint randomly fails to sound

Discussion in 'Scripting' started by Jorhoto, Oct 23, 2019.

  1. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    26
    Hello,
    Im testing PlayClipAtPoint function and it works sometimes and others it doesn't.

    Code (CSharp):
    1. AudioSource.PlayClipAtPoint(Click, Camera.main.transform.position, 1.0f);

    If instead I use an audio source component it always works.

    Is there any known issue/bug?

    Thanks ;)
     
  2. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    2,219
    As far as I am aware, it tries to create an audio source at that point and if it has problems doing that it fails.

    In general, using a static function like that isnt the best, you are always better off supplying a specific audio source so you get some granular control over what is happening. Its also terrible for memory.

    I cant think of any use case where you would not want to do that, even if you just want to 'fire and forget' you should still write some sort of audiosource manager first so you can always get a free audiosource to supply it with, and get decent error feedback if one is not available.
     
  3. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    26
    I wanted to do a sound manager with scriptable objects so I wouldn't need to pass it through all my scenes, but if I need an audiosource component, it seems I will need a GameObject for all my scenes :(.