[내일 배움 캠프 Unity 4기] W9D2 06.11 TIL

김용준·2024년 6월 11일
0

내일배움캠프

목록 보기
31/47

Goals

  • Understood the life cycle of Unity

Class inheritance in Unity

There is a life cycle of class in Unity like a below figure. This cycle had cause the problem on the team project. Today, I studied the feature of class in the Unity.

There are two classes shown in code blocks.

public class A : MonoBehaviour
{
  ...
  private void Awake()
  {
    ...
  }
}
public class B : A
{
  ...
  // there is no `Awake` method in B
}

In these case, the B class in Unity will invoke the Awake method on parent;A. To prevent the issue, we have to insert the Awake on B or make the Awake as virtual.

case 1:

public class B : A
{
  ...
  private void Awake()
  {
    // blank method
  }
}

case 2:

public class B : A
{
  ...
  // the A will be modified to `protected virtual void Awake()`
  private override void Awake()
  {
  }
}
profile
꿈이큰개발자지망생

0개의 댓글