Coding Error!

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:

'Assets/Script/PlaySound.js(4,31): BCE0043: Unexpected token: ).’

‘Assets/Script/PlaySound.js(4,32): BCE0044: expecting ), found ‘.’.’

‘Assets/Script/PlaySound.js(4,33): UCE0001: ‘;’ expected. Insert a semicolon at the end.’

So can anyone help me fix this?

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 );
}'

Look this.

1 Like

I’ll try it, Thanks!

Hey I put the code into my script, but I got the following error code: ‘Assets/Script/PlaySound.js(9,3): BCE0044: expecting ‘’', found ‘\r’.’

My bad, delete the last symbol. ( ’ )

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 );
}
1 Like

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.’

Change the variable name from audio to something else.