Audio.play replaced with other code

I’m using Unity 5.4.0f3 (for various reasons) instead of the one recommended for the course, along with Visual Studio.

audio.Play() was “red lined” in VS but Unity helpfully offered to auto-update my scripts using outdated APIs, and converted it to:

    GetComponent<AudioSource>().Play();

which works.

8 Likes

There are a few functions which have to be changed when doing the earlier sections.

Off the top of my head, the code to change scenes, get external components, and possibly some of the audio code has changed.

For the most part, unity will automatically update it for you, if you agree.
You’ll come across it a couple of times.

I have not run into Unity changing code for me automatically. I always have to go into the forum to figure out how to change the code. How can you get it to change automatically?

When you use Visual Studio, the compiler tells you what you have to use instead.

1 Like

Thank you!

Also, who else belly laughed when he said “so go ahead, find your crack, and stick it in”? (referring to the attachment of the audio file).

makes me 5 years old but man, caught me off guard. 8:35 in. Worth it.

2 Likes

I used this same method.

Sometimes Unity offers to update it for you, other times Visual Studio gives you a potential change. You can also highlight the code and press ALT+Enter to see a suggested change too.

Thanks Todd !

Thanks for the code.
Also using unity 5, Visual Studio on a Mac did not fit it for me, but it is better than monodevelop!

Yes, thank you! I have Unity v.2017.1 and initially had the following, per the course update (https://docs.google.com/document/d/1NkRIbKGZ5X3WbiWa3C4JFN7CAijLQBX-onJGoRagtD4/edit):

AudioSource audio = this.gameObject.GetComponent<AudioSource>();

But it doesn’t work!

What ended up working was just this:

this.gameObject.GetComponent<AudioSource>().Play();

Privacy & Terms