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
1
Question by Remyx · Sep 01, 2016 at 06:01 AM · c#audiotriggergetcomponent

GetComponent().Play(); Question(s)

Before I begin I've looked around and haven't found the answer or hint I've been looking for. My problem is that for some reason my audio will not play specifically if it's in the OnTriggerEnter function.

Example:

void OnTriggerEnter(Collider other) {

     GetComponent<AudioSource>().Play();
     Instantiate(explosion, transform.position, transform.rotation);

     if(other.tag == "Player")
     {
         Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
         Invoke("loadScene", 3.0f);
     }
     other.gameObject.SetActive(false);
     gameObject.SetActive(false);
 }

This yields no sound played whereas my other function for another sound does work and it looks like this

 public void TakeDamage(int amount)
 {
     GetComponent<AudioSource>().Play();
     currentHealth -= amount;
     healthSlider.value = currentHealth;
     if(currentHealth <= 0)
     {
         //Destroy(gameObject);
         Instantiate(explosion, transform.position, transform.rotation);
         gameObject.SetActive(false);
         Invoke("loadScene", 3.0f);
     }
 }

Looking around, I've suspected that the problem has to do with OnTriggerEnter but I'm not 100% sure. Most questions have the old method of playing audio that does not work now like audio.play so I haven't had much luck lurking.

Also, another question about GetComponent().Play();. Is there a simple way to put multiple audio sources on a game object and play specific ones? I read explanations like you need an array to hold sounds but I'm completely new to using audio in Unity so I'm not too sure where to go with that information. For example, in the second block of code TakeDamage. I would like to play not only the sound of being hit (the first sound line of the function), but also when it is destroyed (right before Instantiate).

Thank you for your time in reading this and helping.

Comment
Add comment · Show 1
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 Remyx · Sep 01, 2016 at 03:23 PM 0
Share

Figured out the second part but not the first part yet. Still can't get sounds from the OnTrigger but found out how to play multiple sounds.

2 Replies

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

Answer by Remyx · Sep 02, 2016 at 03:08 PM

Okay so the problem was that I was setting the game object to false so the sound couldn't play. All I had to do was move the audio source to another game object outside of what I was setting active to false and voila! Silly silly problem. My mistake had nothing to do with OnTriggerEnter really.

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
1

Answer by sparkzbarca · Sep 01, 2016 at 06:19 AM

do you understand how a trigger works and is it triggering in scene?

a trigger in order to trigger so this would go off, you need a collider attached to the gameobject this script is attached to and you need the IS trigger box checkmarked in the collider settings in the editor so it has a trigger attached to be triggered.

then you need to make sure the object your running into as it were also has a collider and such so that meets the requirements so you can actually trigger it.

lastly yes you can use an array of sounds.

An array is just really a list of things that are the same and you use mostly the number of each to reference it.

so

 Sound mysound;
 
 mysound = //boom sound

is a single sound.

but if you have say

 Sound mysound1 = //boom sound
 Sound mysound2 = //welcome sound
 Sound mysound3  = //bye sound

you can store them all together like this

 Sound[] mysounds;

Sound mysound declares a single object

 Sound[] mysound

declares an array of that object. it's just a way of saying multiple of the same type of thing. there all sounds, but it's a group of them.

 Sound[] mysounds = new Sound[3];

this declares an array and says how big it is, size is 3 items.

now i can take that array and fill it with three objects. the one thing to note is the "first" object in a list is the zeroth object kinda. Computers generally start counting from zero and because of this when you talk about the first object in a list it's basically item zero not one.

so

 mysounds[0] = mysound1;
 mysounds[1] = mysound2;
 mysounds[2] = mysound3;

this looks a little confusing due to the numbering of the variables as opposed to the array and honestly this is something that happens and you need to be aware of, sometimes it makes sense to count from 1 and other times 0 and when you transition between those systems you just have to be aware.

generally though you aren't going to change the computer, mostly you should try to yourself just embrace this starting to count from zero mechanic yourself by thinking of the first thing as the zeroth thing for example. (in my opinion)

you can once you declare an array and fill it just use those same numbers to pull info back out

so

 Play(mysounds[0])

will play the boom sound and

 Play (mysounds[1]) 

will play the welcome sound.

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 Remyx · Sep 01, 2016 at 01:59 PM 0
Share

Hey! Thanks for the reply. The game I'm working on is a top down space shooter and the player's bullet is the trigger. I have the players bullet hitting the object but its not playing the sound. On the other hand, I have a player bullet hitting the enemy and it does play the sound. The only thing different I can see is that one is in the OnTriggerEnter function whereas the other is using it's own function inside the OnTriggerEnter function. Actually, writing this I'll try that and see if it works. As for the array of sounds thank you for that.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Triggering a certain part of an AudioClip, once. 0 Answers

Audio Trigger 3 Answers

Select collider for GetComponent? 1 Answer

Distribute terrain in zones 3 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