Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mokenister · Feb 20, 2016 at 01:49 AM · collisionsound

Play sound on collision doesnt work

I watched like a million tutorials on youtube and every single one is the same, though it doesn't work for me

public class PickUpSound : MonoBehaviour {

 public AudioClip[] audioClip;

 void PlaySound(int clip)
 {
     audio.clip = audioClip[clip];
     audio.Play();
 }
 void OnTrigerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Player"))
     {
         PlaySound(1);
     }
 }

} Now I did put an audio source on the object I put the script on, and I put the sound I want to play on collision and nothing works :( What should I do? Or do I have to make a completely new script? Thanks in advance

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jebemti · Feb 20, 2016 at 10:10 AM

It's possible you are using an incorrect function.

Check your collider and if it doesn't say "Is Trigger" Use OnCollisionEnter instead.

  void OnCollisionEnter(Collision col)
  {
      if (col.gameObject.CompareTag("Player"))
      {
          PlaySound(1);
      }
  }

The other possibility is that you are using audio, which, depending on which version of Unity you are running, has become outdated. If that's the case cache the AudioSource.

 AudioSource audioSource;

 void Awake(){
     audioSource = GetComponent<AudioSource> ();
 }
Comment
Add comment · 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
0

Answer by hexagonius · Feb 20, 2016 at 02:35 AM

missing a "g" in "Trigger"?

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 Mokenister · Feb 20, 2016 at 02:47 AM 0
Share

Well that was dumb on my side xD Now I get another error saying "IndexOutOfRangeException: Array index is out of range." And it brings me to GetComponent().clip = pickUpSound[clip];
Now when I went back into unity a message popped up saying that it's incorrect and it can fix it for me, therefore it turned audio.clip = audioClip[clip]; audio.Play(); into
GetComponent().clip = pickUpSound[clip]; GetComponent().Play(); Should I make this as it was before or leave it like this? Honestly I don't even know what's wrong in there

avatar image
0

Answer by Mokenister · Feb 20, 2016 at 03:06 AM

I was missing an "g" in "Trigger", but now I fixed it and everything should work normally, but when I pick up the object the sound doesn't go off. No error messages pop up, nothing. What's going on? Seems like I kinda fixed the problem but now the sound just doesn't play even though the background music does

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

45 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 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 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

How to make a sound play and have a delay after so it can't be activated again very fast? 1 Answer

How to reproduce a sound when my character collide 1 Answer

onCollision explode and sound help 0 Answers

Falling Objects Sound 1 Answer

Collider + multiple sounds script 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