I’m trying to add an audio file to my game, and I keep getting this error:
‘Assets/Script/PlaySound.js(4,5): BCE0144: ‘UnityEngine.Component.audio’ is obsolete. Property audio has been deprecated. Use GetComponent() instead. (UnityUpgradable)’
This is my code:
'var soundToPlay:AudioClip; function OnTriggerEnter () { audio.PlayOneShot(soundToPlay); }’
Now since I’ve had a similar error before, I attempted to fix it by using this code:
'var soundToPlay:AudioClip; function OnTriggerEnter () { GetComponent().PlayOneShot(soundToPlay); }’
But I had no luck in doing so, as I got the following errors:
Hi, i think you have to asign GetComponent to a var. Like this.
var soundToPlay:AudioClip;
var audio : AudioSource;
function OnTriggerEnter () {
//First asign to a variable.
audio = GetComponent.<AudioSource>();
//The you use PlayOneShot //second value is the volume.
audio.PlayOneShot(soundToPlay, 1f );
}'
var soundToPlay:AudioClip;
var audio : AudioSource;
function OnTriggerEnter () {
//First asign to a variable.
audio = GetComponent.<AudioSource>();
//The you use PlayOneShot //second value is the volume.
audio.PlayOneShot(soundToPlay, 1f );
}
Well, I put it in the script, but I got another error: 'Assets/Script/PlaySound.js(7,5): BCE0004: Ambiguous reference ‘audio’: PlaySound.audio, UnityEngine.Component.audio.’