singleton unity

C#
public class Example
{
public static Example Instance{get; private set;}

void awake()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
} else
{
Destroy(gameObject);
}
}

}
Source

Also in C#: