RuntimeInitializeOnLoadMethodAttribute

Incorrect usage of “an” in these docs.

It could also do with a better description. A friend of mine was not sure whether his functions would be called after/before every scene load or only once, at the start of the runtime.

Hi Leslie - I posted a bug about this earlier in the month, just forgot to let you know. I’ll take a look and see if there is something easy to do here. (Not something I have used before.)

Ah… So:

  1. Create a new project.
  2. Copy the MyClass.cs into the project. Note it’s not MonoBehaviour related in any way.
  3. Create a cube.
  4. Create a new script file called ExampleScript.cs.
  5. Add the following code into ExampleScript.cs, replacing everything in that file on creation:
using UnityEngine;
using UnityEngine.SceneManagement;

public class ExampleScript : MonoBehaviour {

    MyClass myClass;
    Scene scene;
  
    void Awake()
    {
        scene = SceneManager.GetActiveScene();
        Debug.Log("ExampleScript: Awake: " + scene.name);
    }
      
    void Start () {
        Debug.Log("ExampleScript: Start");      
    }  
}
  1. Add this to the cube.
  2. Run the game.

Hmm. Now not 100% sure that the script should be added to the cube. Conceptually for me it feels more accurate that the script is on the scene, but that’s not do-able. Graham-the-worrier. :frowning:

Okay, so now I am 100% sure that the MyClass.cs script from the doc page doesn’t need to be attached to anything. It’s available to the game start-up. I can’t see any way that the MyClass.cs code can be used by any scene. It’s really about the game starting (only). Line 6 in the script up there should be deleted.