Search Unity

Coding Error!

Discussion in 'Scripting' started by halojman, Oct 4, 2015.

  1. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27
    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<AudioSource>() 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<AudioSource>().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?
     
  2. germangguerci

    germangguerci

    Joined:
    Oct 4, 2015
    Posts:
    6
    Hi, i think you have to asign GetComponent to a var. Like this.

    Code (JavaScript):
    1. var soundToPlay:AudioClip;
    2. var audio : AudioSource;
    3.  
    4. function OnTriggerEnter () {
    5. //First asign to a variable.
    6. audio = GetComponent.<AudioSource>();
    7. //The you use PlayOneShot //second value is the volume.
    8. audio.PlayOneShot(soundToPlay, 1f );
    9. }'
    Look this.

    http://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html
     
    halojman likes this.
  3. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27
    I'll try it, Thanks!
     
  4. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27

    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'.'
     
  5. germangguerci

    germangguerci

    Joined:
    Oct 4, 2015
    Posts:
    6
    My bad, delete the last symbol. ( ' )

    Like this:


    Code (JavaScript):
    1. var soundToPlay:AudioClip;
    2. var audio : AudioSource;
    3. function OnTriggerEnter () {
    4. //First asign to a variable.
    5. audio = GetComponent.<AudioSource>();
    6. //The you use PlayOneShot //second value is the volume.
    7. audio.PlayOneShot(soundToPlay, 1f );
    8. }
     
    halojman likes this.
  6. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27

    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.'
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Change the variable name from audio to something else.