Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I use RuntimeInitializeOnLoadMethod correctly?

Discussion in 'Scripting' started by Deleted User, May 30, 2022.

  1. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LoadBeforeScene {
    4.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    5.     public void Method() {
    6.         Debug.Log("Hello.");
    7.     }
    8. }
    9.  
    I'm not sure why this doesn't log anything. How is this actually supposed to be used?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Since it's not in MonoBehavior, I don't see why it would run at all. How is it supposed to call the method if there's no LoadBeforeScene objects that can call it?

    The documentation all uses static methods, which I assume circumvents this problem, but I believe it would also work if it was just on an in game object.
     
  3. ZetanHwang

    ZetanHwang

    Joined:
    Oct 11, 2016
    Posts:
    7
    [RuntimeInitializeOnLoadMethod] only works on static and parameterless method
     
    Deleted User likes this.