Hi, today I tried to write a script that plays a sound when you push a button and stops it when you stop pushing that button... The problem is that the function "AudioSource.Stop" seems to be an incorrect expression, but I found it on http://unity3d.com/support/documentation/ScriptReference/AudioSource.Stop.html ... So, I think that there could be some older expressions that aren't used in unity anymore... :?
... But when I used it in my code, there was written: Code (csharp): var speed = 2.5; var openSpeed = 2.5; var useTime = 6.0; var fallSpeed = 1.0; var useKey : String; var wheel : Transform; var rotate : boolean = true; var door : Transform; var openDoor : boolean = true; var fall : boolean = false; var wheelSound : AudioClip; var playSound : boolean = true; var doorStart : Transform; var doorEnd : Transform; function OnTriggerStay () { if (Input.GetKey (useKey)) { if (rotate == true) { wheel.Rotate (0, speed, 0); } if (openDoor == true) { door.Translate (0, openSpeed, 0); } } if (door.transform.position.y >= doorEnd.transform.position.y) { openDoor = false; } if (Input.GetKeyDown (useKey)) { if (rotate == true) { if (playSound == true) { AudioSource.PlayClipAtPoint (wheelSound, transform.position); playSound = false; } } } if (Input.GetKeyUp (useKey)) { audio.Stop; } } function OnTriggerExit () { fall = true; opendoor = false; playSound = true; Update (); } function Update () { if (fall == true) { door.transform.Translate(0, -fallSpeed, 0 * Time.deltaTime); } if (door.transform.position.y <= doorStart.transform.position.y) { openDoor = true; fall = false; } } @script RequireComponent (AudioSource)
I feel compelled to point out that the sample code in the docs says "audio.Stop()"...always pay attention to the docs.... --Eric