- Home /
Can you fix my code? (Audio for doors)
Hello, as you know I have really poor skills in JavaScripting and such, although I have learned several things, but this I have spent hours on.. I'm basically trying to make it play an open sound, when the OPEN animation begins, and the same for the CLOSE animation. All I simply want is two sound variables which I have done and want them to play when the doors do their job.
Please let me know if you can solve this code. Thank you.
pragma strict
var doorObj:GameObject;
var isOpen = false;
var delay : float = 0.3;
var openSound : AudioClip;
var closeSound : AudioClip;
function OnMouseDown(){
Debug.Log("Mouse is down");
if(!this.isOpen){
doorObj.animation.Play("NewOpen");
this.isOpen = true;
if (openSound)
audio.PlayOneShot(openSound);
}else{
doorObj.animation.Play("NewClose");
this.isOpen = false;
if (closeSound)
audio.PlayOneShot(closeSound);
}
}
function Start() {
}
function Update() {
}
What's not working? You do have a Collider on the door object, right?
Everything is working, just I can't get the sounds to play when the animations play.
You can see in this image below, I have the parent and then the doors below which contains the two animations to be played: NewOpen and NewClose.
Have you double checked that openSound
and closeSound
have clips assigned in the inspector?
Answer by KellyThomas · Jan 11, 2014 at 02:31 PM
From the screenshots it looks like you are missing the AudioSource component on those doors.
Your code looks correct. Here is what I think is going wrong but I could be incorrect.
You have the script on the parent door which calls for the animations. However the animations don't exit on the parent, they exist on the newtestdoors.
So what I think might work is to call the animations on the newtestdoors ins$$anonymous$$d. something like this (I did not try it but its better than nothing)
var doorObj : GameObject;
var isOpen = false;
var delay = 0.3;
var openSound : AudioClip;
var closeSound : AudioClip;
var Animation : anim; //declares this variable to be of type Animation
fuction Awake()
{
anim = GameObject.Find("newtestdoors").GetComponent(Animation);
}
function On$$anonymous$$ouseDown(){
Debug.Log("$$anonymous$$ouse is down");
if(!isOpen){
anim.Play("NewOpen");
isOpen = true;
audio.PlayOneShot(openSound);
}else{
anim.Play("NewClose");
isOpen = false;
audio.PlayOneShot(closeSound);
}
}
Your script looks correct buddy, although I'm getting Assets/Scripts/door.js(6,17): BCE0018: The name 'anim' does not denote a valid type ('not found'). And I'm too bad at scripting to figure out what that even means! Hahaha! Thanks for trying though!
NEW UPDATE: You fixed it! I forgot to put AudioSource components in the game objects! Thank you man, brilliant!!
Looks like "From the screenshots it looks like you are missing the AudioSource component on those doors."
That was indeed the correct problem, I didn't even put any AudioSource components in my GameObjects!
Answer by Psycho8Vegemite · Jan 11, 2014 at 11:43 AM
This should work, personally I've never used 'this' in any of my scripting so I'm not sure what it does but this will defiantly work if its attached to the door with animations on it.
var doorObj : GameObject;
var isOpen = false;
var delay = 0.3;
var openSound : AudioClip;
var closeSound : AudioClip;
function OnMouseDown(){
Debug.Log("Mouse is down");
if(!isOpen){
animation.Play("NewOpen");
isOpen = true;
audio.PlayOneShot(openSound);
}else{
animation.Play("NewClose");
isOpen = false;
audio.PlayOneShot(closeSound);
}
}
Also you can't ask if("AudioClip"), unless you were trying to see if its null (vars filled in) just put if("Variable" == null){ then if you don't have an audio clip in it, it won't turn up as a error, or if your seeing if it's playing put if(audio.isPlaying){.
I wish I could say it work, but I'm afraid I couldn't get it to work. It asked me for an animation on my main parent object, when the animations are supposed to be on the object "newtestdoors"
I tried putting the animations on the parent object, but then that didn't work.
Your answer
Follow this Question
Related Questions
Play from a variety of sounds? 1 Answer
Can you make this work? (Click Collider = Make Sound) 2 Answers
Play sound on animation event? 3 Answers
Sprinting Audio Problem 1 Answer
GameObject reacts to audio source 1 Answer