Current posts are too complicated. No need to load levels or change colors or shoot bullets. I just want to play a sound when I click on an object in a 3D scene (not a menu). Been searching for a simple answer for 4 hours. All I need is the full script to do this. (Links to complicated explanations aren’t of help.) Thanks
Here we go:
using UnityEngine;
using System.Collections;
public class AudioPlay : MonoBehaviour{
AudioSource ac;
void Start (){
ac = GetComponent<AudioSource> ();
}
void OnMouseDown(){
ac.PlayOneShot (ac.clip);
}
}
Add this script to your GameObject in scene and also add Collider component and AudioSource component. Then add sound clip that you want to play in “Audio Clip” slot of the audio source.
This will work.
(Note : Not Tested)
Hi. I made a new script, cut and pasted your code, and the script on the object. I added an audio source to the object and dragged the audio clip into AudioClip. Added a box collider component to the object, Got a compiler error when I tried to play.
Thanks for responding. It tells me I need to fix compiler errors before I can put the script on the object…pix below…
I have an audio source component on the object with a clip in the AudioClip box, unclicked “Play on Awake”, and I have a box collider (I’ve tried “Is Trigger”, and not “is trigger.”)