- Home /
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.
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.
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.
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.
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
Follow this Question
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