I am a noob to C# and Unity in general, and I am having difficulty trying to play a sound.
So it’s pretty simple, I have a collider object with attached audio source. When it’s clicked, it’s supposed to play a sound and destroy, so I had this:
void OnMouseDown()
{
GetComponent<AudioSource>().Play();
Destroy (gameObject);
}
The problem with this, the sound wouldn’t play because the object was being destroyed. That makes sense. So I started searching for another method. I found AudioSource.PlayClipAtPoint() doing searches for similar issues. This sounds perfect but I can not get it to work.
So the object I have is a roach, it has the audio clip “splat” attached to it. On the roach script, I have the following code:
using UnityEngine;
using System.Collections;
public class roachCode : MonoBehaviour {
public float speed = 30;
void Start()
{
// Initial Velocity
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}
void OnMouseDown()
{
AudioSource.PlayClipAtPoint(splat, transform.position);
Destroy(gameObject);
}
}
I keep getting the error "The name ‘splat’ does not exist in the current context. I have tried creating a gameobject Audio Source to put onto my scene and I named it “splat” but still same problem. I tried changing the “splat” to “roach,” still same issue. I don’t know what I am doing wrong here. How can I get this command to recognize splat?
Thank you for any help you can provide.