- Home /
Phone Ringing audio scripting help
Hi, I'm trying to make a phone ring and when you get near it the ringing stops and a voice audio takes its place. I just can't seem to do it though. I wrote the script but it says "Play" isnt part of Unity Engine Audio Clip. Here is my script:
function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.collider.gameObject.tag == "hippo") { myAudioFile.Play();
}
}
Answer by Eric5h5 · Apr 08, 2010 at 02:31 AM
Play() is for an audio source, not an audio clip. You have to assign the audio clip to the audio source if you haven't already, then do audio.Play()
, assuming that the audio source you want to play is attached to the same object that the script is. Or if the audio source is on the other object:
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.collider.gameObject.tag == "hippo")
{
hit.collider.audio.clip = myAudioFile;
hit.collider.audio.Play();
}
}
$$anonymous$$y script is attached to my Player since its collision, i need my player to like touch the phone and then the audio plays. So can you throw that by me again? $$anonymous$$y audio clip is attached to the source.
Thanx that worked, I only have one problem- turning the ringing off when I touch the "hippo" game object. Please help
@DTJ Productions: If the ringing is being produced by the "hippo" object's audio source, then replacing it with "myAudioFile" like the script already does should stop the ringing.
That did not help, I have a noise playing "On Awake" and need it to stop the time I touch the object or the time the other audio turns on.
Answer by Design3.com · Apr 08, 2010 at 03:59 AM
You need to access the Audio Source Component of the phone GameObject, then tell that to play. I think this will work:
private var phone : GameObject;
function Awake() { phone = GameObject.Find("Phone"); }
function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.collider.gameObject.tag == "hippo") { phone.audio.Play();
}
}
Or you can make "phone" a public variable and set it in the Editor.
Hey I was wondering for the OnControllerColliderHit function and it says phone.audio.Play(); Do I name my audio sound that I want to play "phone"?
Oh and also (sorry for these idiotic questions) I was wondering what to tag the object I want to touch and play the sound, and what to name it. thanks
See Eric's answer. I think it'll work better for you in this case.