how do I make this happen?

looking through as many posts and unityanswers as possible, I’ve come to some code I cobbled together which seems to utterly fail at my simple task lol.

What i want is “when animation X occurs, play sound Y”

the code i wrote, which i know is horribly wrong, is:

var dooreffect : AudioClip;

function playSound(){
	Debug.Log("door_L_open");
	audio.clip = dooreffect;
	audio.Play();
	
	}

the debug.log thing came from another post about sounds and anims, but i know is completely wrong.

Also, I have placed the script and the sound in the door, and dragged my sound file into the variable for the dooreffect.

I get no errors, but I also get no sound when the animation occurs.

you should use: audio.PlayOneShot(dooreffect); and attach an audio source Component to your door.

var dooreffect : AudioClip;

{

if animation.Play("door_L_open");
     audio.PlayOneShot(dooreffect);
}

is that right?

anyone?

what are you trying to do?

I have a script that makes an object slide to one side when the player approaches it. What I want is for a sound to play when the animation occurs.

Your code is basically right from what I can tell, but I don’t touch Javascript.

You need to make sure that particular code is actually being called during the animation.

I’m too new at coding. I;'ve no diea how to do this /headdesk

Can you post the code you’re currently using for this? (The code you posted previously isn’t valid, so I’m assuming that’s not actually what you’re using.)

that was my best attempt at the code and yes its not valid. I just can’t figure how to compose the code that will play the sound file when “door_L_open” animaton plays

When you call the animation, just use the PlayOneShot function immediately after it:-

animation.Play("door_L_open");
audio.PlayOneShot("dooreffect");

How would it know where to find dooreffect in this case as it is formatted like a string? PlayOneShot takes an AudioClip for the first argument. Was that just illustrative?

Did you ever assign an actual sound to that dooreffect?

this script is in my player controller:

function OnControllerColliderHit(hit : ControllerColliderHit)
{
	if(hit.gameObject.tag == "doorL")
	{
		hit.gameObject.animation.Play("door_L_open");
	}	
	if(hit.gameObject.tag == "doorR")
	{
		hit.gameObject.animation.Play("door_R_open");
	}
}

each door has its own tag, door_L and door_R. Each door has its own animation called, respectively, “door_L_open” and “door_R_open.”

Of course, i only want one sound to play once, not a sound for each door or it will be too muddled. Just one sound effect that sounds like 2 doors sliding open. So it only needs to watch for one door animation. Arbitrarily, I’ll pick door_L_open.

so, would this be the total and correct code?

function OnControllerColliderHit(hit : ControllerColliderHit)
{
	if(hit.gameObject.tag == "doorL")
	{
		hit.gameObject.animation.Play("door_L_open");
		audio.PlayOneShot("dooreffect");
	}	
	if(hit.gameObject.tag == "doorR")
	{
		hit.gameObject.animation.Play("door_R_open");
	}
}

–edit–

clearly not, as it generates this error:
“Assets/scripts/door.js(6,34): BCE0023: No appropriate version of ‘UnityEngine.AudioSource.PlayOneShot’ for the argument list ‘(String)’ was found.”

ok, so it looks like I need the “dooreffect” script instead, to tell it that when animation door_L_open plays, to play the sound. But I can’t seem to figure out how to write it properly. I have a different script that plays the sound when the door is hit, but as you hit the door it plays the sound a grillion times because you’re still touching the door. Best to, instead, make it play when the animation occurs. But how to do that?

this is what I have for the on collision

var DoorSound : AudioClip;

function OnControllerColliderHit (hit : ControllerColliderHit)

{

if(hit.gameObject.tag == "door") {  

AudioSource.PlayClipAtPoint(DoorSound,transform.position);

}

how do i reformat this so instead of onhit its onanimation played? Even this code is wrong somewhere as it generates “Assets/scripts/dooreffect.js(12,1): BCE0044: expecting }, found ‘’.”

…I just so wasn’t cut out to code lol.

You’re missing a closing curly bracket to your function.

  • If you put your cursor next to a curly bracket in your text editor then it will highlight that brackets partner. This is handy to make sure everything is closed properly.
  • If there is only 1 line to execute after your expression you don’t need curly brackets at all.
  • PlayClipAtPoint takes at least 2 arguments (the clip and the position (third optional argument for volume))

Anybody can code, it’s not a skill with which one is born. You have to develop the discipline to mind your formatting and syntax, that’s all it is. If you truly believe you cannot code, then you will never be able to code.

I’d like to think that. I remember buying visual c++ 10 yrs ago. I sat down with 1500 page book which began, “This book assumes you are already fully conversant with another programming language.” Since nothing was willing to teach me from the ground up, and everything referred to things I didn’t know, I found it impossible to do anything but sort of guess at rules and syntax. Even now, as I try again with javascript, I look at the 3d platformer tutorial here from unity. Despite saying “go from newcomer to mastery” its PDF starts with, “This tutorial makes extensive use of scripting so you should be familiar with at least one of the supported scripting languages: JavaScript, C# or Boo.” I feel like I need a tutorial to take the tutorial lol.

Ah well, it will happen, I suppose, but I wish I had a resource to learn this from the ground up, assuming I know absolutely nothing which is roundabout what I know lol.

Anyway, so I’m missing a closing curly bracket. Not sure where that would go, but i definitely see 2 opening brackets and only 1 closing bracket. As far as the editor goes, I seem to be getting a different editor than I’m seeing in tutorials. Not sure if this is because im using unity 3 pro? In tut vids I see an editor that highlights words in different colors when correctly written. Mine, UniSciTE, shows everything in blue and just sometimes will make certain words boldface, and others not. I’m not sure if I need to specifically select an editor maybe? This is just the one that defaults when I double click a script.

At any rate, the above script isn’t the right script. If properly written, it will mak a sound when I touch the object. I need it to instead make a sound when the object’s animation plays. Since I have the script that trigger the animation when I touch it, if the sound plays when the animation plays, I will get my one sound played once.

Can anyone show me the right script for this?

You should start at the beginning with C#. Also joining a group might help you learn.

in the meantime, while I spend four years trying to learn C# and then javascript and then Unity 3d, while at the same time 3d modelling, texturing and designing the entire game --can anyone tell me the right code to use lol?

i dunno if it is right …but i think it is what andeeee said like this :

and maybe for after dooreffect you might have to use an = sign without the : sign

no dice. i get this error:

“Assets/scripts/dooreffect.js(3,18): BCE0023: No appropriate version of ‘UnityEngine.AudioSource.PlayOneShot’ for the argument list ‘(String)’ was found.”

I give up. 7 days of trying to get a door to open and make a sound at the same time.
god forbid i try anything trickier than makign a door open lol.