- Home /
Question by
conconexplosion · Aug 07, 2013 at 03:59 PM ·
audioslenderpages
Audio Wont Play In Page Script
I am using a code from majestics slender kit.... I am editing it to play music and stop it depending on how many pages have ben collected. the audio wont play for some reason... anyone know what i did wrong???? i am new to coding so please be patient with me... thanks
#pragma strict
@script RequireComponent( AudioSource )
var pages : int = 0;
var pagesToWin : int = 8;
var distanceToPage : float = 2.5;
public var pagePickup : AudioClip;
var theEnemy : EnemyScript;
public var FirstPage : AudioClip;
public var thirdpage : AudioClip;
public var FifthPage : AudioClip;
public var SeventhPage : AudioClip;
function Start()
{
Screen.lockCursor = true;
// find and store a reference to the enemy script (to reduce distance after each paper is collected)
if ( theEnemy == null )
{
theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
}
}
function Update()
{
//if ( Input.GetMouseButtonUp(0) ) // use E in editor as LockCursor breaks with left mouseclick
if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) )
{
//var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // always cast ray from center of screen
var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
var hit : RaycastHit;
if ( Physics.Raycast( ray, hit, distanceToPage ) )
{
//if ( hit.collider.gameObject.tag == "Paper" )
if ( hit.collider.gameObject.name == "Page" )
{
pages += 1;
//Debug.Log( "A Page was Collected. Total papers = " + papers );
audio.PlayClipAtPoint( pagePickup, transform.position );
Destroy( hit.collider.gameObject );
if ( pages==1 )
audio.PlayClipAtPoint( FirstPage, transform.position );
}
if ( pages==3 )
{
audio.Stop();
audio.PlayClipAtPoint( thirdpage, transform.position );
audio.Play();
}
if ( pages==5 )
{
audio.Stop();
audio.PlayClipAtPoint( FifthPage, transform.position );
audio.Play();
}
if ( pages==7 )
{
audio.Stop();
audio.PlayClipAtPoint( SeventhPage, transform.position );
audio.Play();
}
// make enemy follow closer
theEnemy.ReduceDistance();
}
}
}
function OnGUI()
{
if ( pages < pagesToWin )
{
GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + pages.ToString() + " Pages" );
}
else
{
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Pages Collected!" );
// Application.LoadLevel( "sceneWin" );
}
}
Comment
Can you clarify, what are you expecting to happen and what is actually happening?
Do you have the audio source attached to the same object as the first script?
@awesomeaustin316 @perchik I $$anonymous$$ade an epic fail because i forgot to put in the audio XD.. but now the audio wont stop.... any help? Edit: now it only plays where the page picked up... wtf am i doing wrong?
Your answer
![](https://koobas.hobune.stream/wayback/20220613111717im_/https://answers.unity.com/themes/thub/images/avi.jpg)