DllNotFoundException only on first run

I’ve been working with plugins for some time now but recently I’ve encountered a problem I’ve not come across before.

In the editor, I get a DLLNotFoundException the first time I try to start. After getting this error, if I stop and start again everything works fine. If I restart Unity then I get the same error on on the first time I run.

In standalone this has the effect that the Dll is never found as every time you start the application is the first time.

Anyone heard of this problem before? Any ideas as to what it could be? The Dll is definitely there and it does find it on subsequent runs in the editor.

Thanks.

Right, caught it. Realised my mistake. The plugin in question has a a dependency on another Dll. It was that Dll which was causing the error. I needed to call a function of the dependent Dll first so that it is loaded before calling anything in my own Dll.

I knew all this of course, I’d dealt with it before.

For others who have this symptom, there is another way it can happen. Unity does not search Plugin folders until they have been “imported” (at least in version 2019.4.3f1). So if Unity is rebuilding its Library folder and you try to call a DLL very early, e.g. with [UnityEditor.InitializeOnLoadMethod], it likely will not be found.