Hey everyody, I’m working currently on the -CoinPickupSystem- in my game, but when I try the game, when I pick up a coin, the sound doesn’t get “loaded”
Here’s the code, and can someone please tell me whats going on here??? Thanks!
Hey everyody, I’m working currently on the -CoinPickupSystem- in my game, but when I try the game, when I pick up a coin, the sound doesn’t get “loaded”
Here’s the code, and can someone please tell me whats going on here??? Thanks!
I know this is a little old, but in case someone is looking for the answer to this in the future I’ll answer it now.
You’re using SetActive, which means it’s not going to allow the sound to play after that’s happened. You need to deactivate it’s renderer and collider instead, like so:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class CoinPickup : MonoBehaviour
{
new AudioSource audio;
public AudioClip impact;
public Renderer rend;
void Start()
{
audio = GetComponent<AudioSource>();
rend = GetComponent<Renderer>();
rend.enabled = true;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
rend.enabled = false;
audio.PlayOneShot(impact, 1f);
GetComponent<Collider2D>().enabled = false;
Destroy(gameObject, 10f); //remove this line if you don't want it to be destroyed after 10 seconds.
}
}
}
Hope that helps someone, good luck!
I tested this out and it’s working well! Though, I excluded the line: “[RequireComponent(typeof(AudioSource))]”. Is there a reason why we should include this? Thanks