Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by $$anonymous$$ · Jul 20, 2013 at 08:08 AM · audioaudiosourceaudioplay

HELP! Audio Source Problem

i want access AudioSource from other object

  1. MainObject

    var audio: AudioClip;

    @script RequireComponent.getComponent(AudioSource)

    function Update(){

      audio.Play(); 
     
     }
    
    
  2. Sub Object (how to access AudioSource from main object)

     var audioSource: AudioSource; //i put the main object here
         
         var myAudio: AudioClip;
         
         function Update(){    
         
             audio.Play(); //doesn't work    
         
         }
    
    

NB: sorry for my bad english

Comment
Add comment
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
0
Best Answer

Answer by lucky_luke · Jul 20, 2013 at 11:48 AM

Does your first script even compile? I think if you use JavaScript you should write:

 @script RequireComponent(AudioSource)

instead of

 @script RequireComponent.getComponent(AudioSource)

It's also probably a bad idea to play the audio in the Update() function as this will restart your audio every frame and you won't hear anything. For testing you should better write it into the Start() function so it's only played once. I think you also get an error or a warning if you name a variable "audio".


As you wrote you want to access the AudioSource from another object, the first script is not necessary in this case. All you have to do is to add an AudioSource to the main object. Then in the script for the sub/child object, you need to assign the audio file to the AudioSource and then play the file. Check this script:

 #pragma strict
 
 var audioSource : AudioSource; // Reference to the AudioSource
 var myAudio : AudioClip; // The clip you want to play
  
 function Start() {
     audioSource.audio.clip = myAudio; // Assigns your clip to the AudioSource
     audioSource.audio.Play(); // Plays the clip assigned to the AudioSource
 }



There are also other ways to play the audio, for example using the PlayOneShot() function:

 #pragma strict
 
 var audioSource : AudioSource;
 var myAudio : AudioClip;
  
 function Start(){
     audioSource.audio.PlayOneShot( myAudio );
 }
Comment
Add comment · Show 1 · 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
avatar image $$anonymous$$ · Jul 20, 2013 at 01:08 PM 0
Share

thanks very much

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.

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

Play local Android/IOS audio files in unity game. 0 Answers

How can I play audio clips depending on the players movement 0 Answers

Stop audio from looping and play at lower volume 1 Answer

Can not play a disabled audio source 2 Answers

Problem with playing sound on Trigger-Enter 2 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