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.

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

    // 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];

I hope this helps!


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.