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 FusionSausage · Oct 12, 2013 at 12:02 PM · cameragameobjectaudioplay

Play audio clip on camera from another gameobject

Hi, I have two Audio Sources on my camera. One is playing music, and the other one is playing a sound when I click on the surface. I have a script on my terrain object which is checking if I click on the terrain, and if I do it's supposed to play the little "ding" sound. I've positioned the sound on the camera to hear it good, and now I wonder how I can assign that clip to be activated by another gameobject, my terrain. I though I would just have to drag the audio clip to a variable and play by using audio.PlayOneShot(sound1);, although this didn't work, because it seems like I can't drag an Audio source from one gameobject to another. It's important to hear the sound good, so that's why it has to be 2 sources on the camera.

Can you please tell me what I should do to play ONE audio source on my camera from a script on my terrain?

Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Oct 12, 2013 at 01:52 PM

You could have two ways of doing it.

First you can have a second audio source on your camera. IF you have only one audio source, it should be for the music, if you swap the audio clip, you lose the music.

So you have a second one on your camera but bam, if you use GetComponent you are not sure which it will return.

So to fix this you could have an empty game object (name it ClickOnCam for ex) attached to the camera with the Audiosource. Then your script goes as:

 AudioSource audioSource;
 public AudioClip clickClip;
 void Start(){
    audioSource = Camera.main.transform.Find("ClickOnCam").GetComponent<AudioSource>();
    audioSource.clip = clickClip;
 }
 void Update(){
    audioSource.Play();
 }

the other way is to use

 AudioSource.PlayClipAtPoint(clickClip, Camera.main.transform.position);

this will create a new Audio source object that will get destroyed automatically once the clip is done.

Comment
Add comment · Show 10 · 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 FusionSausage · Oct 12, 2013 at 01:59 PM 0
Share

And this script should be placed on the original camera (main) or the new camera?

avatar image fafase · Oct 12, 2013 at 02:03 PM 0
Share

No this is placed on the object that check for the input, you mentioned a click on the terrain. Wherever this is checked, this above is added.

avatar image FusionSausage · Oct 12, 2013 at 02:16 PM 0
Share

@fafase it doesn't look like it gets the AudioSource component, Debug log says null and it doesn't show up in editor. I placed the ClickOnCam as a child to $$anonymous$$ain Camera and the script was placed in my terrain script CursorGrid2... Any idea?

avatar image fafase · Oct 12, 2013 at 02:55 PM 0
Share

Is ur cam tagged as $$anonymous$$ain Camera?

avatar image FusionSausage · Oct 12, 2013 at 03:02 PM 0
Share

Now it is, but it's still saying null... Super wierd!

Show more comments
avatar image
1

Answer by Gjallanhorn · Oct 12, 2013 at 01:34 PM

You can resolve this easily, in import settings of your audio file, just uncheck "3d sound" and add the audiosource to your terrain.

But, to play another Audiosource in Camera..

 AudioSource[] allAudios = Camera.main.gameObject.GetComponents<AudioSource>();
 allAudios[1].Play();

allAudios is a array with all audiosources in the camera, so if it doeen't play the right one, just change the index in array... allAudios[0], allAudios[1], allAudios[2] etc..

Comment
Add comment · Show 2 · 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 fafase · Oct 12, 2013 at 01:53 PM 0
Share

How do you know the component returned is not the one playing the background music?

avatar image Gjallanhorn · Oct 12, 2013 at 01:56 PM 0
Share

Testing...

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

16 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 avatar image

Related Questions

audio source not playing the audio clip 2 Answers

Audio sound on gameObject delete? 1 Answer

Using an Audio Listener on an Audio Source 1 Answer

Play audio when gameobject is near 2 Answers

Audio won't play 1 Answer


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