• 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
Question by Ice Koobs · May 07, 2013 at 04:43 AM · javascriptaudioreduce

Quick way to enter and condense repeated lines of code

Hiya everybody

I was wondering if there was a way to condense and also reduce the time it takes to repeat some lines of code?

In my current script I need to call different sound effects for different situations.

So far what I've come up with the following

 for(var sfxObjects : GameObject in GameObject.FindGameObjectsWithTag("SFX"))
             sfxObjects.BroadcastMessage ("SoundPlayer", jumpSound);

This is pasted into any script after lines that cause an action that needs a sound effect (CharacterControl for jumping, powerUpControl for when items appear etc). It sends a message to a SFX holder near the camera (so the sound can come out clear and isn't cut off if the object is destroyed) which hold the following script.

 #pragma strict
 //to play all SFX in game
 //to be attached to SFX Holder
 
 @script RequireComponent(AudioSource)
 
 
 function SoundPlayer (playedSFX : AudioClip)
 {
     audio.PlayOneShot (playedSFX);
 }

So I've managed to reduce it down a little. I was just wondering if it was possible to reduce it more in JavaScript/UnityScript.

For example make the BroadcastMessage lines of script into something seemly like

 BroadcastSFX([Insert SFX Variable Here]);

Thank you for your time and I look forward to reading your answers.

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Ice Koobs · May 07, 2013 at 11:41 AM

Thank you Martin and while your answer is genius. There are a few problems with it. I need it in JavaScript. Not because I don't understand C# but because the person I'm doing it for doesn't. Also I don't seem to be clear in what I'm explaining. There is only one SFX object in the scene. I just use the For loop to find it. I guess I should do:

 sfxObject = GameObject.FindGameObjectWithTag("SFX");

However it was late when I wrote this and the code up but with bright eyes and a little sleep, I've found my answer. Thank you again though for your help.

All I need to do is declare it as the following at the beginning of each script that will call ay SFX

  var sfxObject : GameObject = GameObject.FindGameObjectsWithTag("SFX");
 //Find the sfxHolder in the scene

Then for any sfx the script may need to deploy declare them

  var jumpSound : AudioClip;
  var stepingSound : AudioClip;
  var landingSound: AudioClip;
  //All the sound files needed by current script

When it needs to be called in the main body of the script

  sfxObject.SendMessage ("SoundPlayer", jumpSound);

But then this can be copied into scripts that are attached to different object such as the power ups, enemies , scenery and so forth.

As I mentioned the connecting script attached to the sfxObject has the following lines of script

 function SoundPlayer (playedSFX : AudioClip)
 {
     audio.PlayOneShot (playedSFX);
 }

All in all I reduced it by one line of code for every additional Sound FX called. Which is a lot as there are a lot of Sound FXs in this game.

Comment

People who like this

0 Show 0 · 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

Answer by SubatomicHero · May 07, 2013 at 07:19 AM

Well I'm not sure why you would want to reduce your lines of code count...you only have a handful anyways. If youre talking of thousands of lines of code then I could see the merit in it, but otherwise why bother?

But I will say that you shouldn't loop through each SFX in your scene.

I personally would control the SFX playing by using states and put all your SFX you want to play for your player into an AudioClip[] array (public) either attached to your player for 3D sound) or your camera, whichever you prefer.

Then create an enum like this (C#):

 public enum playerState
 {
     idle, isRunning, isJumping, hasPowerUp etc etc;
 }
 public playerState pState;

Then you can switch through the states that you change i.e if the player jumps for a few seconds change the state to isJumping. Then in an update function check it like so:

 switch (pState)
 {
     case playerState.isJumping:
     {
         // play jumping sound here
         // after jump animation has occured change state back to idle
         break;
     }
 
     // other states here
 }

Obviously you will know if your jumping sound is at the firs element of your array (element [0]) so you always play that when jumping. You could also have separate arrays for each movement type (jumping, running etc) and play a random sound from each bank to give a nice audio touch to your game.

 public void playRandomJumpingSound()
 {
     int random = Random.Range(0, JumpSFXArray.Length);
     audio.Clip = JumpSFXArray[random];
     audio.Play();
 }

I hope this helps!

Martin

Comment
Fattie
Philipp
tomtom789

People who like this

3 Show 3 · 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 Fattie · May 07, 2013 at 07:56 AM 1
Share

a generous answer

avatar image SubatomicHero · May 07, 2013 at 08:08 AM 0
Share

Thanks Fattie :D

avatar image SubatomicHero · May 07, 2013 at 12:06 PM 0
Share

Well I'm glad I helped in someway :)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

14 People are following this question.

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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How to get sound to play on certain condition (javascript) 2 Answers

How to create an auido array (JavaScript) 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


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