Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by djdrool91 · Aug 17, 2012 at 06:32 AM · c#audiosoundmusic

Audio Scripting

Hey everyone,

I have a whole load of sound files that I made for a game I'm working on and am wondering how to use the audio system built into unity because I it's my first time and I don't really understand the documentation. I managed to attach audio files and control them using scripts but the audio always gets cuts off when the projectile gets destroyed.

A solution that I think would work is to load all the audio into a script and use it as a manager for my audio files. So this way I think that I can load the sounds here and then use it from other scripts but what I don't know is how to set up the prefabs with audio sources so that I can individually tweek the values for different objects but at the same time play audio on top of one another without cutting anything off or even cutting off the audio when a projectile gets destroyed for example.

Do I need to dynamically create an object with the audio in it and have it move with the prefab projectile for example and let it play?

 using UnityEngine;
 using System.Collections;
 //A Compressed version of the actual code
 
 public class SoundManager : MonoBehaviour {
  
     public GameObject soundClip;
 
     public static AudioClip
       
         //According to file in explorer
         //Enemy [Movement]
         EnemyAirSpawn, EnemyGroundSpawn, EnemyDeath, EnemyBaseMove, EnemyHeavyMove, EnemyCrusherMove, EnemyKamikazeMove, EnemyTurretMove,
         
         //Enemy [Weapons]
         EnemyBomb, EnemyMortar, EnemyMortarImpact, EnemyPlasma, EnemyPlasmaImpact, EnemyTesla,
     
     void Awake()
     {
         string Path1 = "Sounds/Enemy/Movement/";
         string Path2 = "Enemy/Weapons/";           
 
         //Path 1 sounds
         EnemyAirSpawn = (AudioClip)Resources.Load(Path1 + "EnemyAirSpawn");
         EnemyGroundSpawn = (AudioClip)Resources.Load(Path1 + "EnemyGroundSpawn");
                  
         
          //Path 2 sounds
         EnemyBomb = (AudioClip)Resources.Load(Path2 + "EnemyBomb");
        
         EnemyMortar = (AudioClip)Resources.Load(Path2 + "EnemyMortar");
         EnemyMortarImpact = (AudioClip)Resources.Load(Path2 + "EnemyMortarImpact");
         
         EnemyPlasma = (AudioClip)Resources.Load(Path2 + "EnemyPlasma.ogg");
         EnemyPlasmaImpact = (AudioClip)Resources.Load(Path2 + "EnemyPlasmaImpact.ogg");
         
         EnemyTesla = (AudioClip)Resources.Load(Path2 + "EnemyTesla");   
     }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DannyB · Aug 17, 2012 at 07:11 AM 0
Share

In addition to my answer below, a friendly advice: It will be much easier for people to help you if a) your question is short and clear and b) your code is properly formatted and contains only the $$anonymous$$imal bits needed for people to understand / reproduce the problem.

avatar image djdrool91 · Aug 17, 2012 at 07:21 AM 0
Share

Thank you and yeah haha I was unsure about how to input code but I fixed the code placement and I'll try to cut it down. Thanks! =D

avatar image DannyB · Aug 17, 2012 at 08:25 AM 0
Share

Dude... shorten the code! :)

This almost looks like a full game... heh.

avatar image djdrool91 · Aug 17, 2012 at 08:29 AM 0
Share

There we go! =p

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DannyB · Aug 17, 2012 at 07:09 AM

To avoid sound cutoff, do not destroy the object until it has finished playing the sound. Instead, hide it on impact and destroy it after a few seconds.

 audio.Play();
 gameObject.renderer.enabled = false;
 Destroy( gameObject,2 );
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image djdrool91 · Aug 17, 2012 at 07:20 AM 0
Share

Hey thanks for the reply! Yeah I was thinking about that too but I'm going to port this onto a mobile game with many projectiles at once, do you think this is the best way?

avatar image DannyB · Aug 17, 2012 at 07:30 AM 0
Share

I think it is a good way, and maintenance wise, it is probably the preferred way.

Of course, there is always the option of a "manager", but consider the implications, mainly:

  1. You will have to place it where the actual sound happens, or at least "in the neighborhood" for 3D sound positioning.

  2. You have another foreign object to handle, you will need communication to and from other objects.

  3. You will not be utilizing the intended (or at least natural) Unity design, being: object has an audio source that can be played at will, Unity takes care of the rest.

Lastly, if for some reason the "lazy destroy" method does not work for you and you are sure it cannot be corrected, I would still try to avoid a manager, and ins$$anonymous$$d, just spawn an empty object in the place of impact, which does a simple thing: Play audio on instantiation, and "commit suicide" after X seconds, so at least you will have a lightweight object there.

If you want to avoid having too much of these, you can use some object recycling method that makes sure there are only X number of "audio objects" on the screen, but I think this is way down the road, if all else fails.

avatar image djdrool91 · Aug 17, 2012 at 07:56 AM 0
Share

Thanks for your answer once again. I think you're right and I will now proceed to try it out.

However I'm wondering because I'm confused but is this the way to do it?

  1. Declare AudioClip.

  2. Assign it in the inspector

  3. Include an AudioSource component in the prefab

  4. (This is the part I'm confused about) Do I use audio.Play(), audio.PlayOneShot(Clip) or do I use AudioSource.PlayClipAtPoint(myClip, transform.position). Because my intention is to have layered sounds with many many objects at the same time (I'll mix it later) which one do I use?

avatar image DannyB · Aug 17, 2012 at 08:23 AM 0
Share

Well, this is what I do:

  1. Add an Audio Component to my prefab object.

  2. Select a sound file as the Audio Clip, and disable the Play on Awake checkbox.

  3. Then, in the script that is attached to the object I do audio.Play()

If I want the object to play one of several possible sounds, I either declare an AudioClip Array in the script, or declare several AudioClip variables, ( e.g. public AudioClip[] explosions; ) and then before audio.Play() I use audio.clip = explosions[2] or something similar.

You may use PlayOneShot() when you have an AudioClip variable to pass.

avatar image djdrool91 · Aug 17, 2012 at 08:30 AM 0
Share

Thanks! well I'm trying out pretty much in theory what you suggested and it's going O$$anonymous$$ so far but I'll report back when I'm complete.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Turn Off Sound For Certain Objects? 1 Answer

Audio Play Once 2 Answers

Trigger audio loop on beat with PlayScheduled 1 Answer

How do I fix audio loop delay 2 Answers

Mute volume / sound problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges