- Home /
Error UnityEngine.Component.audio is obsolete.
I received a few errors when i am using this script trying to get audio to work, and i think it has to do with using the old version of unity before. Here they are
Assets/Scripts/Shooting.js(69,25): BCE0144: 'UnityEngine.Component.audio' is obsolete. Property audio has been deprecated. Use GetComponent() instead. (UnityUpgradable)
Assets/Scripts/Shooting.js(69,31): BCE0019: 'playOneShot' is not a member of 'UnityEngine.Component'.
I dont know what is going on, but if there is another way to play the audio that would be great.
function Shoot(){
while(true){
if(shooting == true && weaponnum == 0 && rbullets1 > 0){
rbullets1--;
audio.playOneShot(aeksound, 1.0);
Physics.Raycast(transform.position, transform.forward.hit);
Debug.Log(hit.collider.tag);
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle"){
Destroy(hit.transform.gameObject);
}
}
else if(weaponnum == 0 && rbullets1 == 0){
yield WaitForSeconds(2);
if(rbullets >= 30);
rbullets -= 30;
rbullets1 += 30;
}
else if(rbullets < 30){
rbullets1 += rbullets;
rbullets = 0;
}
}
if(shooting == true && weaponnum == 1 && pbullets1 > 0){
pbullets1--;
audio.playOneShot(walthersound, 1.0);
Physics.Raycast(transform.position, transform.forward.hit);
Debug.Log(hit.collider.tag);
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle"){
Destroy(hit.transform.gameObject);
}
}
else if(weaponnum == 1 && pbullets1 == 0){
yield WaitForSeconds(2);
if(pbullets >= 30);
pbullets -= 30;
pbullets1 += 30;
}
else if(pbullets < 30){
pbullets1 += pbullets;
pbullets = 0;
}
}
Answer by Ian-McCleary · Jun 12, 2015 at 02:34 AM
To those looking for the answer. This worked just fine.
AudioSource.PlayClipAtPoint(walthersound, transform.position);
I dont know if it allows others to hear the audio though.
@Bunny83 also stated that i didn't capitalize the P in Play. This function also works
GetComponent(AudioSource).PlayOneShot(aeksound, 1.0);
Note that PlayClipAtPoint creates a temporary AudioSource to play the sound. As it's a static method it's not bound to a certain gameobject. When the clip has been played the temporary AudioSource is destroyed.
@Bunny83 Yes, i noticed this. Thank you for posting that last reply of yours. I didn't realize it changed it that much and i also didn't realize that i didn't capitalize The P in Play. Thank you for your help!
Answer by crohr · Jun 10, 2015 at 09:58 PM
That is just telling you to use GetCommponent()
whoops my code didn't show up:
GetComponent<AudioSource>().playOneShot(aeksound, 1.0);
After changing it, I get these set of errors in the two different places that i changed the code.
Assets/Scripts/Shooting.js(49,51): BCE0043: Unexpected token: ).
Assets/Scripts/Shooting.js(49,52): BCE0044: expecting ), found '.'.
Assets/Scripts/Shooting.js(49,53): UCE0001: ';' expected. Insert a semicolon at the end.
Now i do have semicolons and to prove it, here is my code. $$anonymous$$aybe i wrote something else wrong?
function Shoot(){
while(true){
if(shooting == true && weaponnum == 0 && rbullets1 > 0){
rbullets1--;
GetComponent<AudioSource>().playOneShot(aeksound, 1.0);
Physics.Raycast(transform.position, transform.forward.hit);
Debug.Log(hit.collider.tag);
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle"){
Destroy(hit.transform.gameObject);
}
}
else if(weaponnum == 0 && rbullets1 == 0){
yield WaitForSeconds(2);
if(rbullets >= 30);
rbullets -= 30;
rbullets1 += 30;
}
else if(rbullets < 30){
rbullets1 += rbullets;
rbullets = 0;
}
}
if(shooting == true && weaponnum == 1 && pbullets1 > 0){
pbullets1--;
GetComponent<AudioSource>().playOneShot(walthersound,1.0);
Physics.Raycast(transform.position, transform.forward.hit);
Debug.Log(hit.collider.tag);
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle"){
Destroy(hit.transform.gameObject);
}
}
@Bunny83 So would it look like GetComponent(AudioSource).aeksound;
? because trying this does not work. nor does GetComponent.().aeksound
I now get this error Assets/Scripts/Shooting.js(49,54): BCE0019: 'aeksound' is not a member of 'UnityEngine.AudioSource'.
Im still getting used to the way all the functions work
$$anonymous$$y two sound variables are defined as var aeksound : AudioClip; var walthersound : AudioClip;
Uhm why do you have changed your code completely? GetComponent returns a reference to the AudioSource component that is attached to the same object as your script. In the past there was the "audio" property which has done this under the hood for you.
So ins$$anonymous$$d of audio.xxxx you use GetComponent(AudioSource).xxxx where "xxxx" is of course whatever you want to access from the AudioSource component.
For example "PlayOneShot". Note that method names usually start with a capital letter so your "playOneShot" doesn't exist at all, not in the current version of Unity, neither in an older version.
GetComponent(AudioSource).PlayOneShot(aeksound, 1.0);
Your answer
Follow this Question
Related Questions
Calling a function from another script issue 1 Answer
error javasript 1 Answer
How am I getting this error?? 1 Answer
Confusing Error 1 Answer