유니티

김동현·2022년 7월 7일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LogTest : MonoBehaviour
{
    void Awake()
    {
        Debug.Log("Awake()");
    }

    void OnEnable()
    {
        Debug.Log("OnEnable()");
    }

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Start()");
        // 에러 로그
        // Debug.LogError("테스트를 위한 에러 로그");

        Destroy(gameObject, 3f);
    }

    void FixedUpdate()
    {
        Debug.Log("FixedUpdate()");
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("Update()");
    }

    void LateUpdate()
    {
        Debug.Log("LateUpdate()");
    }

    private void OnDisable()
    {
        Debug.Log("OnDisable()");
    }

    void OnDestroy()
    {
        Debug.Log("OnDestroy()");
    }
}

이벤트 함수 호출 순서

  • Awake() -> OnEnable() -> Start() -> FixedUpdate() -> Update() -> LateUpdate -> OnDisable() -> OnDestroy()

Destroy() 메소드

destroy(gameObject, 3f) -> 3초 뒤에 gameObject 를 폭파시킨다. 
hierarchy 에서도 사라진다.

?. 연산자

private void OnTriggerEnter(Collider other)
    {
        PlayerHealth playerHealth = other.GetComponent<PlayerHealth>();

        if (playerHealth != null)
        {
            playerHealth.Die();
        }

        other.GetComponent<PlayerHealth>()?.Die();

    }

이게 같고

if (playerHealth != null)
     {
         playerHealth.Die();
     }

     playerHealth?.Die();

같은 의미이다. 널체크할 때 쓰면 좋다.

profile
해보자요

0개의 댓글