Sound doesn't play on Trigger???
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!
please post the actual code and not a screenshot of it - it's harder for somebody to help you if they need to use the code - nobody is going to type it in ;)
check out the user guide/faq for information on how to do that if you're unsure.
Answer by S_Byrnes · May 16, 2016 at 09:20 AM
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!
Answer by davi2tu · Aug 13, 2018 at 07:17 PM
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 :)
Your answer
Follow this Question
Related Questions
My character won't stop crouching, they dont stop crouchin 2 Answers
2d platformer physics 0 Answers
Cannot play sound on collision 1 Answer
Independent player weapon system 0 Answers
I have a problem playing an AudioSource 2 Answers