[SOLVED] Other way of making music player

During the lecture there was a mini challenge with some hints how to make music player. I came up with this idea:
https://pastebin.com/kygZXUz2

It works, but I’m not sure if Ben’s code is better in some way. I would be happy if anyone can explain benefits of doing it in the way presented during the lecture.

1 Like

It does work, although, if you’re going to do it that way, you should use a bool. You’re essentially using count as a bool and setting it to either 1 or 0. It’d be more clear what you’re doing if you had a bool called isCreated or something along those lines and set it to true or false.

In general though, keeping track of a MusicPlayer instance would be the standard way to go. Especially if you needed to call methods on it or access its properties. In this game, we just call GameObject.DontDestroyOnLoad(gameObject);, and we don’t actually use the MusicPlayer for anything, so your example works fine…but imagine if we wanted to change the volume of the MusicPlayer's AudioSource at run-time for some reason…then we’d need to keep track of the instance.

2 Likes

Privacy & Terms