0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UdonSharpでAwakeすな

Last updated at Posted at 2025-03-09

結論

  • Awakeは使用せず、OnEnableメソッド以降で行う
    • 理由: Awake内で変数への代入や関数(例: Debug.Log)の実行はできるが、変数への格納情報はAwake外までは保存されない
      • → Monobehaviourと動作が異なるため、意図しないバグの原因になりかねない

事象

  • 変数の初期化処理をAwakeで行っても、変数に指定した初期値が保存されていなかった

開発環境
image.png

事象の確認結果

先に結果を提示しておく。

VRChat SDK使用時のUnityEditor

  • Awake時でawakePos変数をLogを表示すると(0,2.605f,0)だが、Update内で同じ変数awakePosを表示するとAwake時と異なり(0,0,0)になっている
    image.png

VRChat SDK未使用の通常のUnityEditor

  • Awake時でawakePos変数をLogを表示すると(0,2.61f,0)で、Update内で同じ変数awakePosを表示してもAwake時と同様に(0,2.61f,0)になっている
    image.png

事象の検証手順

~AwakeとOnEnableやStartでのGameObjectTest Init Variables のTransform.Positionの取得動作の違いについて~

VRChat SDKを使用時のUnityでの動作確認

  1. Udonsharpスクリプト TestInitVariables.cs を作成し、空のGameObjectTest Init Variablesにアタッチする。
    • Test Init Variables の座標は今回(0,2.605f,0)のようにインスペクターで指定する
TestInitVariables.cs
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;

public class TestInitVariables : UdonSharpBehaviour
{
    private Vector3 enablePos;
    private Vector3 startPos;
    private Vector3 awakePos;
    private bool hasCalledAwakeLog = false;
    private bool hasCalledEnableLog = false;
    private bool hasCalledStartLog = false;
    private void Awake()
    {
        awakePos = transform.position; // (0,2.605f,0)
        Debug.Log("AwakePos in Awake: " + awakePos); // AwakePos in Awake: (0,2.605,0)
    }
    private void OnEnable()
    {
        enablePos = transform.position; // (0,2.605f,0)
        Debug.Log("EnablePos in OnEnable: " + enablePos); // EnablePos in OnEnable: (0,2.605,0)
    }
    private void Start()
    {
        startPos = transform.position;// (0,2.605f,0)
        Debug.Log("StartPos in Start: " + startPos);// StartPos in Start: (0,2.605,0)
    }

    private void Update()
    {
        if (!hasCalledAwakeLog) { Debug.Log("AwakePos in Update: " + awakePos); hasCalledAwakeLog = true; } // AwakePos in Update: (0,0,0)
        if (!hasCalledEnableLog) { Debug.Log("EnablePos in Update: " + enablePos); hasCalledEnableLog = true; } // EnablePos in Update: (0,2.605,0)
        if (!hasCalledStartLog) { Debug.Log("StartPos in Update: " + startPos); hasCalledStartLog = true; } // StartPos in Update: (0,2.605,0)
    }
}

image.png

2.Unityエディタ上でPlayすることでUdonSimulatorを実行し、表示されるログを確認する
VRChat SDK使用時のUnityEditor
image.png

VRChat SDK未使用の通常のUnityでの動作確認

  1. VRChat SDK使用時と同様 Udonsharpスクリプト TestInitVariables.cs を作成し、空のGameObjectTest Init Variablesにアタッチする。
    • ※ただしVRChat SDKは使用しないため、TestInitVariables.csスクリプトの継承元をMonoBehaviourに直しておくこと
    • Test Init Variables の座標は今回(0,2.605f,0)のようにインスペクターで指定する
TestInitVariables
using UnityEngine; // ← UdonSharpBehaviour用のusingは削除する

public class TestInitVariables : MonoBehaviour // ← Monobehaviourになおす
{
    private Vector3 enablePos;
    private Vector3 startPos;
    private Vector3 awakePos;
    private bool hasCalledAwakeLog = false;
    private bool hasCalledEnableLog = false;
    private bool hasCalledStartLog = false;
    private void Awake()
    {
        awakePos = transform.position; // (0,2.605f,0)
        Debug.Log("AwakePos in Awake: " + awakePos); // AwakePos in Awake: (0,2.605,0)
    }
    private void OnEnable()
    {
        enablePos = transform.position; // (0,2.605f,0)
        Debug.Log("EnablePos in OnEnable: " + enablePos); // EnablePos in OnEnable: (0,2.605,0)
    }
    private void Start()
    {
        startPos = transform.position;// (0,2.605f,0)
        Debug.Log("StartPos in Start: " + startPos);// StartPos in Start: (0,2.605,0)
    }

    private void Update()
    {
        if (!hasCalledAwakeLog) { Debug.Log("AwakePos in Update: " + awakePos); hasCalledAwakeLog = true; } // AwakePos in Update: (0,0,0)
        if (!hasCalledEnableLog) { Debug.Log("EnablePos in Update: " + enablePos); hasCalledEnableLog = true; } // EnablePos in Update: (0,2.605,0)
        if (!hasCalledStartLog) { Debug.Log("StartPos in Update: " + startPos); hasCalledStartLog = true; } // StartPos in Update: (0,2.605,0)
    }
}

2.Unityエディタ上でPlayし、表示されるログを確認する
VRChat SDK未使用の通常のUnity
image.png

Appendix:イベントの実行順序

引用元にも記載されているが、ここにすべてのイベントが表示されているわけではないらし

image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?