30 lines
827 B
C#
30 lines
827 B
C#
using UnityEngine;
|
|
|
|
public class PlayAudioOnCamera : MonoBehaviour
|
|
{
|
|
public AudioClip clip; // assign your audio file in the Inspector
|
|
private AudioSource audioSource;
|
|
|
|
void Start()
|
|
{
|
|
// Get or add an AudioSource to this GameObject
|
|
audioSource = gameObject.GetComponent<AudioSource>();
|
|
if (audioSource == null)
|
|
{
|
|
audioSource = gameObject.AddComponent<AudioSource>();
|
|
}
|
|
|
|
// Assign the audio clip
|
|
if (clip != null)
|
|
{
|
|
audioSource.clip = clip;
|
|
audioSource.playOnAwake = false;
|
|
audioSource.loop = false; // change to true if you want looping
|
|
audioSource.Play();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("PlayAudioOnCamera: No audio clip assigned!");
|
|
}
|
|
}
|
|
} |