29 lines
679 B
C#
29 lines
679 B
C#
using UnityEngine;
|
|
|
|
public class MyAudioPlayer : MonoBehaviour
|
|
{
|
|
private AudioSource audioSource;
|
|
|
|
void Awake()
|
|
{
|
|
// Try to get it from the current object first
|
|
audioSource = GetComponent<AudioSource>();
|
|
|
|
// Fallback: search the scene (safer than lazy Find at Play)
|
|
if (audioSource == null)
|
|
{
|
|
GameObject go = GameObject.Find("AudioSource");
|
|
if (go != null)
|
|
{
|
|
audioSource = go.GetComponent<AudioSource>();
|
|
}
|
|
}
|
|
|
|
// Final safety check
|
|
if (audioSource == null)
|
|
{
|
|
Debug.LogError("AudioSource not found!");
|
|
}
|
|
}
|
|
}
|