2
\$\begingroup\$

I'm making some kind of dependencies solving script. To work this script, it must be run before all other MonoBehaviour scripts.

So I made the script something like this:

public class DependencyController {
    private static Dictionary<string, bool> m_DependencyTable = new Dictionary<string, bool>();

    static DependencyController() {
        SceneManager.sceneUnloaded += ResetDependencyData;
    }

    ... some other stuffs ...
}

I'm using dictionary to compare which dependency script(s) loaded. To can use every script's Awake/Start, I declared as static and instantiated right after.

The system actually work pretty good, all scripts can access DependencyController in Awake/Start without any problem, but have some questions remains:

  1. Is static m_DependencyTable is always created before scene loaded?
  2. Is static DependencyController constructor always called after SceneManager is loaded? As you can see, I attached EventListener in SceneManager.sceneUnloaded so that I can clear the dependency table before scene loads.

Any advice will very appreciate it.

\$\endgroup\$
2
  • \$\begingroup\$ Why are you using Constructor anyway? if you want to load table first then load it in Awake or OnEnable method and load scene on Start method. I think what you are trying to do is very simple but your are over complicating it. \$\endgroup\$
    – Saad Anees
    Commented Nov 24, 2019 at 7:05
  • \$\begingroup\$ @SaadAnees Access another component's value in Start/Awake function sometimes throws null exception because they are not invoked Start/Awake yet. DependencyController is not MonoBehaviour script, is just class that collecting dependent modules are ready or not to check some scripts are ready(means invoked Awake/Start) so that I can access safely regardless order of scripts execution. Using Script Execution Order will work but I prefer to handle it with script, also using script execution order won't worked if I used inheritance. \$\endgroup\$
    – modernator
    Commented Nov 26, 2019 at 13:05

1 Answer 1

1
\$\begingroup\$

In Unity we don't Have such a thing. Instead you have to change script running priority. for example you have many scripts that have Awake methods. just go to: Edit > Project Settings> then select the Script Execution Order.

Just find you resolver script and drag it on top of others scripts. this way you make sure that it runs before all of scripts.

Just put

static DependencyController() {
    SceneManager.sceneUnloaded += ResetDependencyData;
}

on awake of that script that runs before all of scripts.

\$\endgroup\$
4
  • \$\begingroup\$ I don't want to use Script Execution Order. There are some reasons, one main reason is that it won't worked properly if I used inheritance. So I want to control from script as possible, but without using Awake or Start. Put dependency solving logic into Awake or Start look messy, also I don't want to put same logic to every script that requires dependencies. I was solved this problem by using inheritance, so that encapsulate all automated dependency solving problems but using inheritance makes hard to extend other functionalities and had some limits on it. \$\endgroup\$
    – modernator
    Commented Oct 25, 2019 at 2:38
  • \$\begingroup\$ You are working in Monobehaviour and I dont understand why you dont wanna use Awake or Start. Just because it looks messy? No it doesnt. It is how Unity Monobehaviour works. And even if you use awake or start you wont have to write same code on every scrip. You have to maintain your workflow accordingly. \$\endgroup\$
    – Saad Anees
    Commented Nov 24, 2019 at 7:09
  • \$\begingroup\$ @SaadAnees No, I never thought that using Awake or Start is messy. Reason I do this because when I access some value of another component in Awake/Start method, but sometimes they are not ready. Just like this guy said, using "Script Execution Order" will fix this, however using script execution order wasn't worked when I used inheritance, that's why I'm asking here. Of course I can write some code to wait until dependent script is ready(means invoked Awake or Start) by using coroutine, but do this every script looks messy and I'm looking for some better approach about this. \$\endgroup\$
    – modernator
    Commented Nov 26, 2019 at 13:01
  • \$\begingroup\$ @SaadAnees I know how Awake/Start works and when to use it, but the order of Awake/Start method in each script is purely random, so I needed a way to access the value in another component safely. I do use Awake/Start a lot, just find a better way to wait until dependent script is ready or not. \$\endgroup\$
    – modernator
    Commented Nov 26, 2019 at 13:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .