- Home /
Question by
actionjdjackson · Mar 31, 2020 at 08:37 PM ·
audioclipaudio source
What's wrong with my audio source?
The sound "pickupSFX" does not play.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
[RequireComponent(typeof(AudioSource))]
public class InventoryItem : MonoBehaviour
{
public AudioClip pickupSFX;
AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.name == "Player")
{
audioSource.PlayOneShot(pickupSFX, 1.0f);
gameObject.SetActive(false);
}
}
}
Comment
There are potentially other sounds playing at the same time (a footsteps sound) from another object.
Answer by FlaSh-G · Mar 31, 2020 at 09:33 PM
Components only work when they're enabled and on an active GameObject. Since you disable your GameObject, the AudioSource stops working immediately.
Your answer
Follow this Question
Related Questions
Dynamic Backgroundmusic 1 Answer
How to get C# Array to Load into Audio Source 0 Answers
Play multiple audio clips from one object 1 Answer
how to make multiple sound augmented reality in 1 scene UNITY? 0 Answers
AudioSource.clip.time won't work? 2 Answers