- Home /
Question by
kallieinar · Dec 21, 2020 at 10:08 AM ·
2d gameaudiosourceoncollisionentertags
Make different sounds play depending on which item collides with player
CONTEXT: I'm making a game where you try to catch as many fish as you can in a bucket in a minute. However bombs also drop out of the air and im trying to figure out how to play soundeffect1 when a fish goes into the bucket and soundeffect2 when a bomb goes into the bucket.
RIght now soundeffect1 plays when both a fish and a bomb goes into the bucket. plz help.
CODE im using
public class basket : MonoBehaviour
{
public GameObject fish;
public AudioSource flop;
public AudioSource boom;
// Start is called before the first frame update
void Start()
{
flop = GetComponent<AudioSource>();
boom = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Fish")
{
ScoreScript.scoreValue += 1;
flop.Play();
}
if (collision.gameObject.tag == "Bomb")
{
ScoreScript.scoreValue += 1;
boom.Play();
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613021910im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't refer to a child (with a different tag) when the parent has a rigidbody 2d 1 Answer
OnCollisonEnter2D. 1 Answer
OnTrigger bullet with multiple colliders on enemy 1 Answer
Spatial Blend is making sound very quiet. What to do? 0 Answers
How can I play one music right after the other ends? 5 Answers