Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So let's take another look at how we tend to use the singleton in unity.
We're only really using it because if we didn't have the pattern, this object would appear again and
again and again.
Every time we reload the scene or go to a new scene, we would get a new instance and it would also
be set as do not destroy on load.
So what we're looking for isn't really the singleton pattern.
It's just so happens that this golden hammer has been used to solve the problem.
What we're looking for is a way to create an object that is persistent across scenes, and the way I
would prefer to do that is to make that the responsibility of a different Class A, perhaps a persistent
object spawns.
Now, if you've taken the RPG series, you will have come across this because I have introduced that
class over there as well.
But I think it's good to talk about it in the context of the singleton pattern as well.
So what we can do is we can go back to our ambient audio player behavior and I'm going to strip out
the stuff here that is to do with only once initialization.
In fact, I'm going to remove that.
Don't destroy and load all of this stuff.
Let's just make it an ambient audio player behavior, and all it should do is that it has a fade music
method, which is it's only one responsibility.
However, let's introduce the persistent object Spooner now the persistent objects.
Warner does have some static state, but you'll notice that it has little state.
As I can possibly find, it is a persistent object that has a single Boolean.
So it's just saying whether we had spawned or not.
And how does this work?
Instead of trying to spawn a class or anything like that, it spawns a prefab.
So we configure it to spawn a particular prefab, and it will go ahead and do that unless it has already
spawned one.
So what happens is in a week, if we've already spawned one because we've done that in the previous
scene, nothing happens.
Otherwise, we spawn the persistent object and we set that we have spawned to true and you can see down
here and spawn persistent objects.
All it's doing is the instantiating that and setting it to don't destroy on LOTE so that it will continue
to go on through into following scenes.
Now my challenge to you is to go ahead and implement this for our ambient audio player behavior because
as you can see in unity at the moment, if I go and load the scene and I hit reload several times,
what I'm getting is, well, first of all, my singleton hasn't even don't destroy a load.
So assuming that it was then destroy and load, if I had that set up in a week to destroy on load the
game object just the sake of testing it here, we're going to remove that very soon and we go and play
over in unity.
Then we're going to see that when we reload, each time we reload, that new singleton is being created.
So I'd like you to try and use that spawn and mono behavior.
Put it in your scene.
Link it to a prefab.
Create a prefab that is with the ambient audio player, so create an audio player prefab.
Add the spawn into the scene instead and try reloading the scene.
Now, see what happens and see that you can use this kind of pattern to achieve the same effect as the
singleton.
But without a singleton and with far less static state around that static state does need to be there,
unfortunately, but is restricted to this one class, not to the ambient audio player that could now
be unit tested.
It could have all sorts of other good advantages that we don't need to deal with.
We also don't have that global access to it anymore, so it can't just go anywhere in a scene, and
things are a lot better with this setup.
OK.
Welcome back.
Let's give this a stab say, the first thing we're going to do is, as I said, we're going to take
this persistent object spawned and we're going to put it in a scene.
So we're going to create a new empty.
Call it the object, Spooner.
And you could use this for everything that you need to instantiate, and he wants, not just your audio
play, you can put everything that needs to be instantiated only once into a prefab, which is quite
kind of quite nice.
You don't have to worry about doing this singleton pattern again and again and again.
So this is telling me that there is not a match here, so I'm going to fix my own typos here.
That should have been the persistent object spawn, and I think I must have accidentally deleted this
at some point.
Now I'm going to go over and drank this into object spawn in the scene, and then we're going to want
to create a prefab.
There's going to be a persistent prefab, so I'm going to create an empty, persistent objects that's
going to be turned into a prefab after I've created put my singleton example object underneath and a
drag that into anywhere in my assets folder.
At the moment, I'm going to delete it from my scene because I'm going to be spawning it via this persistent
object supporter.
So I pop that in to the persistent object spawn.
Now I can go ahead and hit play.
And now you can see that the persistent object clone is here and don't destroy unload.
We've only got one.
I can go ahead and hit reload, and you can see that no matter how many times I reload that scene,
it's not going to spawn those persistent objects again because of the static state, that static variable.
So that is my personal solution to how I do persistent objects in unity and avoid the use of the singleton
pattern.
I hope you find that useful in your own projects.
Feel free to use that code, adapt it and improve it as you see fit.
And if you do improve it, share it down in the discussions so other students can have a go to.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.