Walk Sound Scripting Help

Hello everyone, I wanted to ask 2 things:

  1. I keep getting an error: Assets/WalkSoundTrigger.js(7,42): BCE0044: expecting :, found ‘;’.
  2. I am a complete noob at scripting and was wondering how I could improve this script in any way.

Result:
What I want this script to do is play a walk sound when the WASD keys are pressed but mute if none of those are pressed.

var WoodWalk : AudioClip;

function OnTriggerEnter (GameObject : Collider)
{
	if (Input.GetKeyDown(KeyCode("W")));
	{
		WoodWalk.AudioClip.Play();
	}
	
	if (Input.GetKeyDown(KeyCode("S")));
	{
		WoodWalk.AudioClip.Play();
	}
	
	if (Input.GetKeyDown(KeyCode("A")));
	{
		WoodWalk.AudioClip.Play();
	}
	
	if (Input.GetKeyDown(KeyCode("D")));
	{
		WoodWalk.AudioClip.Play();
	}
	
	else
	{
		WoodWalk.AudioClip.mute = true;
	}
}

Thanks anyone who helps out :slight_smile:

(GameObject : Collider) won’t work since they are both types… you want a variablename : Collider

How would I go fixing the compiler error?

Go step-by-step through a Unity tutorial (Lerpz is the official tutorial I’d recommend).

One thing to note:
All variables should be camelCase, and not CamelCase.
All functions and classes should be CamelCase.

There’s no functional difference, and you can even do it in reverse (but don’t :P); the point is to visually differentiate classes and variables so you don’t accidentally use one where another should be. Also other programmers can then really easily get into your code without worrying about what’s what. :wink:

Unity itself uses the camelCase convention, and you might notice that “GameObject” is a type.
To declare a parameter, you need:

function X( {parameterName} : {ParameterType} )

“GameObject” would normally be a fine variable name, if hard to read, but Unity already has a GameObject class, so the compiler thinks you’re talking about that. Then it doesn’t know what to do with the colon.

Science! :smile:

Oh, also, improvements -

Instead of {Keycode(“X”)}, use {Keycode.X} if you know what X is - it’s faster. Keycode(String) is for when you’re not sure what the string is going to be, and you need to pass it in.

“OnTriggerEnter” is called exactly one time (per trigger entrance), and “Input.GetKeyDown” only returns true for one frame - it’s highly unlikely you’ll have pressed the key at the exact time the trigger is hit. Try using the Update function, or Input.GetKey (which returns true while the key is pressed).