• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

DontDestroyOnLoad() 가 적용이 안됩니다

23.02.12 19:18 작성 조회수 579

0

코드는 제대로 작성했는데 정작 유니티로 돌아가면 적용되지 않는 이유가 무엇일까요.........

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Managers : MonoBehaviour
{
    static Managers s_instance; //유일성 보장
    public static Managers Instance { get { init(); return s_instance; } } //유일한 매니저를 가져온다

    // Start is called before the first frame update
    void Start() //반드시 부품으로 들어가있을때만 호출 가능함 (즉 monobehaviour이 필요함) 
    {
        //초기화
        //Instance = this;
        init();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    static void init()
    {
        if (s_instance == null)
        {
            GameObject go = GameObject.Find("@Managers");
            if (go == null) //@manangers가 없다면
            {
                go = new GameObject { name = "@Managers" };
                go.AddComponent<Managers>(); 
            }

            DontDestroyOnLoad(go); //웬만해서는 삭제 안됨

            s_instance = go.GetComponent<Managers>();
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Managers mg = Managers.Instance;
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    
}

 

답변 1

답변을 작성해보세요.

0

유니티로 돌아가서 [실행]을 해야지 @Managers가 만들어지는 것입니다.
그런데 이미 @Managers를 수정으로 배치해 놓으셔서,
image이 코드가 스킵이 될 것으로 예상됩니다.