• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by conconexplosion · Aug 08, 2013 at 01:09 PM · audioslenderwont stopslenderman

Music Wont Stop From Page Script+ Extra Problom

T$$anonymous$$s code is from majestics slender kit.... i have edited it to play music just like the slendermans shadow games but for some reason the music wont stop on my code???? i also have a problom where the audio only plays where the page was picked up... why is my code doing t$$anonymous$$s???

     #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 t$$anonymous$$rdpage : 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 $$anonymous$$t : RaycastHit;
     if ( Physics.Raycast( ray, $$anonymous$$t, distanceToPage ) )
     {
     //if ( $$anonymous$$t.collider.gameObject.tag == "Paper" )
     if ( $$anonymous$$t.collider.gameObject.name == "Page" )
     {
     pages += 1;
     //Debug.Log( "A Page was Collected. Total papers = " + papers );
      
      audio.PlayClipAtPoint( pagePickup, transform.position );
       Destroy( $$anonymous$$t.collider.gameObject );
     } 
    if ( pages==1 )
    audio.PlayClipAtPoint( FirstPage, Camera.main.transform.position);}
    {
    }
     if ( pages==3 )
    {
     audio.Stop();
    audio.PlayClipAtPoint( t$$anonymous$$rdpage, Camera.main.transform.position);
    
    }
     if ( pages==5 )
    {
    audio.Stop();
    audio.PlayClipAtPoint( FifthPage,  Camera.main.transform.position);
    
    }
     if ( pages==7 )
    {
    audio.Stop();
    audio.PlayClipAtPoint( SeventhPage,  Camera.main.transform.position);
    
    }
     // 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

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Mill0698 · Aug 08, 2013 at 02:29 PM

There are two common methods for triggering sounds from script.

The first common method to ac$$anonymous$$eve t$$anonymous$$s would be to not have an audio clip variable at all, but instead attach an AudioSource component to your GameObject (or to the pickup object), and drag the "pickup" audio clip reference into that audio source component.

You could then trigger the sound by calling

audio.Play(); from a script on the same object. Or, if you placed the AudioSource on the pickup object, you could call it from your player object when it's collected by using the "otherObject" variable in your OnTriggerEnter function, like t$$anonymous$$s:

otherObject.audio.Play(); However, there is another way to play sounds w$$anonymous$$ch doesn't require an AudioSource component to be attached. If your script has a reference to the audioClip to play (as it does currently), a simple way to trigger the sound is to use PlayClipAtPoint.

You could place t$$anonymous$$s code in your OnTriggerEnter function, near the line where the pickup is destroyed:

AudioSource.PlayClipAtPoint(pickup, transform.position); T$$anonymous$$s will play the sound at the position of the player object (the object that t$$anonymous$$s script is placed on) in the 3D world. Depending on your situation you may want to change t$$anonymous$$s to:

AudioSource.PlayClipAtPoint(pickup, collider.transform.position); (to play the sound at the location of the other object)

or

AudioSource.PlayClipAtPoint(pickup, Camera.main.transform.position); (to play the sound at the camera's location, so that it doesn't sound as though it's coming from any particular location in the 3d world)

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Networked Audio Listeners 2 Answers

Audio on Collision code doesn't work? 2 Answers

My Footstep isn't working 2 Answers

How can I test if an MP3 is a Sine Wave? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges