• 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 randyjama · Aug 18, 2017 at 11:23 PM · collisionaudiotriggercollectible

Cannot get trigger sound to play for collectibles

I cannot get the sound to play for collectibles made from the following script:

public class SpawnerManager : MonoBehaviour { public GameObject prefab; public int numberOfObjects; public float radius; public float heightTimesRadius;

 void Start()
 {
     for (int i = 0; i < numberOfObjects; i++)
     {
         float angle = i * Mathf.PI * 2 / numberOfObjects;
         Vector3 pos = new Vector3(Mathf.Cos(angle), heightTimesRadius, Mathf.Sin(angle)) * radius;
         Instantiate(prefab, pos, Quaternion.identity);
     }
 }

This is my audio script, which I add as a component to my object along with the script above:

public class Collectable : MonoBehaviour {

 void OnTriggerEnter()
 {
     AudioSource source = GetComponent<AudioSource> ();
     source.Play ();
 }

}


I can use the 2nd script to make a random object I create trigger the sound on collision, but I cannot get the spawned objects from my first script to trigger any sound. Advice?

EDIT It seems that the trigger is set to the coordinates in the center of the circle created rather than for each individual collectible that makes up the circle. Additionally, a sound plays immediately as I start the game (since I'm placed in the center of the circle of collectibles) despite having "play on awake" turned off in the audio source component.

Comment
nt314p

People who like this

1 Show 2
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 nt314p · Aug 18, 2017 at 11:29 PM 0
Share

Can you post a pic of the inspector of the prefab?

avatar image randyjama nt314p · Aug 18, 2017 at 11:53 PM 0
Share

alt text

prefab.jpg (132.2 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by unidad2pete · Aug 19, 2017 at 01:11 AM

Maybe next questions are obvious for you, but most problems are obvius problems.

  void OnTriggerEnter()
      {
          AudioSource source = GetComponent<AudioSource> ();
          source.Play ();
      }
 

The scene game Object with this code, have an AudioSource and one audioclip configured, and her collider set to Trigger?

All objects you need to make the Trigger called have a Collider and almost one have a Rigidbody?

Notes: Trigger events are only sent if one of the Colliders also has a Rigidbody attached.

What is exactly the problem? OnTriggerEnter() its never called?

Are you getting any error on console type NullReference?

Comment

People who like this

0 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 randyjama · Aug 19, 2017 at 03:40 AM 0
Share

Well I tried this same method with a completely new collectible and it worked, but the ones I need to work are from the 1st script above, which allows me to place them in a circle rather than by hand. The issue is that, for collectibles made from the first script, no sound occurs when the player object collides with the collectible.

I hope I'm being clear.

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

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

Audio. How to play a sound file when triggered by collision? 3 Answers

Play Animation OnTriggerEnter 2 Answers

OnTriggerExit stop audio 1 Answer

Using shadows as Triggers? 1 Answer

Detecting a Collision between specific two objects? 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