Question by
imauroe · Jan 03 at 07:25 PM ·
collisiontriggerunity 2daudiosource
AudioSource.play() not playing
Hi peps,
So I've been trying to add a sound when a bullet collides with my player, but when it collides I got a message in console "can not play a disabled audio source".
public class InstancePrefabParticles : MonoBehaviour
{
public GameObject prefab;
public float livinTime = 2f;
AudioSource _audioSource;
private void Awake()
{
_audioSource = GetComponent<AudioSource>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
GameObject instance = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject;
_audioSource.PlayOneShot(_audioSource.clip);
Destroy(instance, 2f);
}
}
}
if I try to play the audiosource outside the CompareTag condition it sounds when the bullet instantiate instead of when collides. I've also try things like:
if (collision.CompareTag("Player"))
{
GameObject instance = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject;
if (!_audioSource.isPlaying)
{
_audioSource.Play();
}
Destroy(instance, 2f);
}
imagen-2022-01-03-161236.png
(20.6 kB)
Comment