Constructor being called after Start

I have a class that doesn’t inherit from anything that handles a lot of data (it’s a static class that holds an instance of itself, a singleton) and it does this creation of itself as well as initializing all the data in a constructor called from the static variable declarations. I have an object that references this data holding object (which does inherit from monobehaviour) and pulls data in its start function.

The start function is called before the other object actually initializes its data in it’s constructor. How do I make sure my data holding object executes itself first?

EDIT:
Okay, wtf. I moved my script to run before everything else in the script order window under project settings and it’s still broken.

I have an ItemHandler class and a MachineHandler class. Both are singletons and work pretty much the same way. The weird thing is, even though they are both in the same script, ItemHandler gets called, then the machine tries to make itself, then MachineHandler makes itself even though MachineHandler never calls the script on the machine. The only thing I can see is that MachineHandler loads a prefab that contains the machine script and then initializes some data which might be the problem. The only thing though is that I put a Debug.Log statement at the start of both the MachineHandler’s constructor (before Resources.Load(“”) of the prefab) and at the start of the prefabs Start function and the MachineHandler’s constructor is being called second.

I have no idea what to do. I made the script be called before everything else using the script order window and yet it still says that I define this class AFTER I create the machine even though the only thing that might cause it (the prefab) is farther along in the constructor.

2 Answers

2

Use Awake to initialize the data. All Awakes run before any Starts, and all Starts run before any Updates.

This isn't on a class that inherits from MonoBehaviour, it is on a class that inherits from nothing.

Hmm, okay. Ill look into the script execution order thing or perhaps just make a game object like you said. Is there any way to make a singleton of a Scriptable Object? The thing about my singletons is that they are just kinda out there and I wanted to try using scriptable objects. Scriptable objects cant seem to hold an instance of themselves (like the singletons, not the way I define them at least) and I cannot access them globally like I can with the singleton (because it stores the instance (which I cant store in a scriptable object like I have it now) in a static variable).

I haven't used Scriptables myself. My "statics" that "need" a gameobject are usually monobehaviours loaded up with static functions which just remember the object they're attached to as their 'instance' in their Awake (so Globals : MonoBehaviour and Globals.StartCoroutineX uses the Globals instance object to run a coroutine). Maybe that can adapt to Scriptables too?

Aaah, I see. Alrighty, thanks for the advice

Alright, fixed it, found out the problem. I followed your advice and added the init thing but I found out the real problem was somewhere else in my script and isn't related to this. Thanks for all the help Loius

You said your class is a singleton. How is it implemented? A singleton should initialize itself when it’s accessed. A usual singleton looks like this:

public class MyClass
{
    private static MyClass m_Instance = null;
    public static MyClass Instance
    {
        get
        {
            if (m_Instance == null)
                m_Instance = new MyClass();
            return m_Instance;
        }
    }
    
    private MyClass()
    {
        // constructor
    }
    public void DoSomething()
    {
        
    }
}

Whenever you want to use your class you just use:

MyClass.Instance.DoSomething();

This will create an instance if there’s none yet and call DoSomething on it.

You might want to check your implementation if it actually fits the singleton requirements :wink: