When I use ‘audio.PlayOneShot(…)’ it wants to replace it with GetComponent

Updated on February 27, 2016 in [A] Audio
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on February 26, 2016

Hi there, so i’m still pretty noob to Unity, and I want to implement sounds. But when I try
<span style="color: #444444;”>audio<span style=”color: #444444;”>.<span style=”color: #444444;”>PlayOneShot<span style=”color: #444444;”>(<span style=”color: #f57c00;”>”<span style="color: #f57c00;”>GOsound<span style=”color: #f57c00;”>”<span style="color: #444444;”>);
it says ‘API update required’ or so and replaces it with
GetComponent<span style="color: #444444;”>.<<span style=”color: #444444;”>AudioSource<span style=”color: #444444;”>>().<span style=”color: #444444;”>PlayOneShot<span style=”color: #444444;”>(<span style=”color: #f57c00;”>”<span style="color: #f57c00;”>GOsound<span style=”color: #f57c00;”>”<span style="color: #444444;”>);

How can I fix this?

  • Liked by
Reply
0 on February 26, 2016

Something went wrong… Can post your question again?

  • Liked by
Reply
Cancel
3 on February 27, 2016

#pragma strict

var GOsound : AudioClip;

function OnTriggerEnter (info : Collider) {

if (info.tag == “Player”) {

Debug.Log (“Finish”);

Application.LoadLevel (“Main”);

audio.Clip = GOsound;

audio.Play();

}

}

or the hastebin: http://hastebin.com/yomapipuwu.avrasm

Wise
on February 27, 2016

Not sure exactly how JavaScript works with Unity and in general – but coming from a C# view – don’t you need to define the variable ‘audio’ as an audio source so that you can use audio.Clip and then use audio.Play?

You could probably solve this by writing ‘var audio : AudioSource;’ at the top of your script.

Sorry if I’ve misunderstood something you’ve said or have read the code wrong – but hope I can help!

Guru
on February 27, 2016

You could try (if I am right same as James not comfortable using JS):

GOsound.Play();

Wise
on February 27, 2016

What I’m guessing is that you’re dealing with some outdated part of the API. I think they changed this a while back. You need to use “GetComponent(AudioSource)” at the places you put “audio” now.
You can also, like James said, declare a variable of the type AudioSource, and put the wanted AudioSource in there.

Show more replies
  • Liked by
Reply
Cancel