Reputation:
public class AudioManager : MonoBehaviour
{
private static AudioManager audioManager = null;
private void Start ()
{
if (audioManager != null)
{
Destroy(gameObject);
print(audioManager + "'s self destroy requested.");
}
else
{
audioManager = this;
GameObject.DontDestroyOnLoad(audioManager);
}
}
}
I know this code's function which is if there is an audioManager than destroy the new and than the audioManager only exist once so the audio of the audiomanager doesnt make a new audiomanager and so there wont be loop so i am going to hear the attached audio file only once. But can someone explain me that what is "this" and what is it used for and do the same with "static" keyword? Thanks for the help, Kristóf
Upvotes: 1
Views: 109
Reputation: 1437
this
is a C# keyword that is a reference to the current instance that the code is running in.
static
means there's only one memory location for this variable, meaning even if you had a second instance of this class, this variable would point to the same backing data.
Used together, this is the "singleton pattern." See here for some extra info.
Upvotes: 4