๋ฐ‘์— ๋Œ“๊ธ€๋กœ ๋‹ฌ์•„ ์ค„๋ผ ๊ทธ๋žฌ๋Š”๋ฐ ๊ธ€์ž ์ œํ•œ์œผ๋กœ ๋‹ค ์•ˆ ์จ์ง€๊ธธ๋ž˜...


์›๋ž˜ ๋งŒ๋“ค๊ธฐ๋Š” ์š”์ฝ”๋กฌ ๋งน๊ทธ๋ž๋Š”๋ฐ


public class SingleTonใ€ˆTใ€‰ : MonoBehaviour where T : MonoBehaviour
{
protected static T _instance = null;
public static T instance
{
get
{
if (_instance == null) _instance = Object.FindObjectOfTypeใ€ˆTใ€‰();
if (_instance == null)
{
var object_ = new GameObject(typeof(T).Name + " Object");
_instance = object_.AddComponentใ€ˆTใ€‰();
}
return _instance;
}
}
}


๊ทธ๋ƒฅ ์š”๋กœ์ฝ”๋กฌ ์”€
public class SingleTonใ€ˆTใ€‰ : MonoBehaviour where T : MonoBehaviour
{
protected static T _instance = null;
public static T instance
{
get
{
if (_instance == null) _instance = Object.FindObjectOfTypeใ€ˆTใ€‰();
return _instance;
}
}
}


์‚ฌ์šฉ.

public class GameManager : SingleTonใ€ˆGameManagerใ€‰
{

[SerializeField]

private Text _scoreText = null;





P,s. OnDestroy์—์„œ _instance = null; ํ•ด์ค˜์•ผ ๋‹ค์Œ ๋ฒˆ ์”ฌ ๋กœ๋“œ ๋•Œ ์ฐธ์กฐ ์—๋Ÿฌ๊ฐ€ ์•ˆ๋‚˜์š”.