1.  FindObjectOfType<T>();

- 이름이 같아야 함
- 여러 개면 첫번째 찾은 거 참조

- 비활성 오브젝트는 접근 불가 ㅜㅜ 실행 순서 생각하면서 써라..

 

> 시작 시, Go 상태였다가 Con 매서드를 호출해 Stop으로 상태를 바꾼다.

private enum State는 스크립트에서만 상태를 바꿀 수 있다.
public State state; 이면 필드가 생긴다.



 

  • App
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using UnityEngine;

public class App : MonoBehaviour
{
    void Start()
    {
        Controler controler = FindObjectOfType<Controler>();
        controler.Control();
    }
}

 

  • Controler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controler : MonoBehaviour
{
    private enum State
    {
        Go, Stop
    }

    private State state;

    // Start is called before the first frame update
    void Start()
    {
        state = State.Go;
        Debug.Log($"start State: {state}");
    }

    public void Control( )
    {
        state = State.Stop;
        Debug.Log($"Con State: {state}");
    }
}

Control 매서드를 private로 선언하면 참조 불가

 

이건 불가

public enum State {...} / private State state; 는 가능

 

+ Recent posts