- Home /
Walking sound Script returns with Error: BCE0077
I'm new to unity and i have some Problems at creating a Script that plays a sound when pressing "w" "a" "s" "d" and stops it when releasing those. Here's the Script so far:
#pragma strict
var Sound : AudioClip;
function Start () {
}
function Update () {
if(Input.GetKeyDown("w"))(Input.GetKeyDown("a"))(Input.GetKeyDown("s"))(Input.GetKeyDown("d"));{
audio.clip = Sound;
audio.Play();
}
if(Input.GetKeyUp("w"))(Input.GetKeyUp("a"))(Input.GetKeyUp("s"))(Input.GetKeyUp("d"));{
audio.Stop();
}
}
This keeps returning with the error: it is not possible to invoke an expression of type 'boolean' (BCE0077)
I would apreciate any help and also if somebody could tell me how to make the sound stop if i jump tat would be nice too. Thanks in advance!
Answer by FrankStarsKo · Sep 05, 2013 at 10:22 AM
just replace your code with this, and attach your audio source to the game object
#pragma strict
@script RequireComponent(AudioSource)
function Start () {
}
function Update () {
if(Input.GetKeyDown("w")||Input.GetKeyDown("a")||Input.GetKeyDown("s")||Input.GetKeyDown("d"))
{
audio.Play();
}
if(Input.GetKeyUp("w")||Input.GetKeyUp("a")||Input.GetKeyUp("s")||Input.GetKeyUp("d"))
{
audio.Stop();
}
}
Hope this is what you wanted.
Thanks for the quick answer but the Script doesn't allow me to bind in an Audio clip.. or am i missing something ?
Edit: got it fixed by making a variable... now the only problem is the sound restarts everytime I press another button ex. I'm holding down "w" the sounds starts playing then I don't let go of "w" but I also start holding "d" then the sound restarts any way around that ?
i tested the script before i sended to you, and i saw there was not need to make the variable (if you just added, it is there with no use) you can safely leave it as i gave it to you,
in the other hand, maybe your sound is too long, try to cut your sound to only 1 step in the file, after doing that go to your gameobject that holds the audiosource and set to "loop" checked and "Play On Awake" uncheked inside the inspector tab.
one last thing, if you want to make your sound to stop while jump, i think it would be easier to mix the script i gave you with your character controller script so you can be able to check for the condition "isGrounded"
if (controller.isGrounded)
{
audio.Play();
}
otherwise your code is going to turn too complex to follow. @_@ cheers.
Answer by robhuhn · Sep 05, 2013 at 09:56 AM
You missed the logical operators in your condition, closed the bracked too early and have a semicolon behind the if statement.
Here are a few examples of logical operators:
Thanks alot, could you please elaborate where i have an ; to much and where i ned to reset the Bracket (and do you mean these () or {}?)