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. Dismiss Notice

Is it practical to instantiate sound prefabs? :)

Discussion in 'Scripting' started by Rutenis, Jun 7, 2014.

  1. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    So, yeah, the title says what iam thinking. So Is it PRACTICAL to instantiate sound prefabs? Because i usually do it. :)

    -Thanks!
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Yeah, I guess it is. I don't really see anything impractical about it.
     
  3. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Well I don't know exactly what you call a "sound prefab" but if it is what I think it is, then depends, if your game is like a FPS or a 3rD person shooter where the location of the sound source matters then ok, if is something like a 2D game then there is absolutely no reason to have more than one sound source.

    I usually use a singleton as a "sound Manager" which holds all the sounds and music of the game and also plays and listen them, which I can call with a single line of code from any script.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It does matter for a 2D game; you'd want sounds coming from objects on the right to actually come from the right, for example.

    --Eric
     
  5. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    But that would enter in my description of games where the location of the sound source matters , but anyways I have done tons of 2D platformers and never needed to have sounds playing from anywhere but if you really want to have that feature then ok, I don't.
     
  6. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Well, by saying sound prefab i mean whenever i use sound i instantiate a copy of the sound:) I wanna now, will it make the game run slower or not? Im creating a mobile game btw. :)
     
  7. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    You mean like for example if you had a gun in the game every time you fire the gun you instantiate a sound with its own source and everything? don't do that, instead have the gun have its own audio source and and use PlayOneShot() to play the sound like this:

    Code (CSharp):
    1. public AudioClip GunSound;
    2. ....
    3. //To play the sound
    4. audio.PlayOneShot(GunSound);
     
  8. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297

    Oh okay, i tried it and it works perfectly, plus its better. Now i will always use audio.PlayOneShot. Thanks man! :)
     
    Last edited: Jun 8, 2014
    raycosantana likes this.