0
votes

Here's my code, it's so basic and I know the solution will be too, but I checked in reference and still didn't fix it, any suggestion will be appreciated, thanks!

using System.Collections;

using UnityEngine.Audio;

using System.Collections.Generic;

using UnityEngine;

public class Passed : MonoBehaviour {

AudioSource audioData;

void Start()
{

}
void OnColliderEnter2D()
{
    audioData = GetComponent<AudioSource>();
    audioData.Play(0);
}

}

1
What are you playing?jiveturkey
Any error? What is the exact issue? Do you have Collider2D components on your objects? Do you have a Rigidbody2D on at least one object?derHugo

1 Answers

1
votes

Have you created in your scene a Game Object: Audio Source and attached the audio? Then you attach that Audio Source created with your audioData.

On the other hand, I would write the code like this:

void Start()
{
    audioData = GetComponent<AudioSource>();
}
void OnColliderEnter2D()
{    
    audioData.Play();
}

Also make sure the collision is being detected.