how to achieve faster code execution in unity?

hello.

before i start;

-using visual studio to write code

  • writing codes in c#

  • using unity to develope games/apps

so i was working on a project. using unity 4.++

then i decided to upgrade to 2019 version.(am using free version)

in the previous version … my games are able to run without any issue;

after upgrading… when i tried to run my game(in editor) i got some error. after checking i found out that

some codes are being executed rather slow. i will explain what i mean.

code example;

public class AAA : scriptableobject

public string name;
public string d;
public string f;
public string j;
public string g;

etc..

prefab
public class BBB : monobehaviours // this is attached to prefab

public string name;
private string b;
private string c;
private string d;

public void  setup( AAA aaa)
{
        name = aaa.name;
        b = aaa.d; 
        etc etc
       /// just assigning value to the variables above from AAA
}


public class CCC : monobehaviour

list<AAA> object = new list<AAA>() {   about 100 items in here   };

void start()
{
      foreach (AAA aaa in object)
      {
            var newobject = (instantiate prefab , here i use resourcesload)
             newobject.GetComponent<BBB>().setup(aaa);
             
               // im using debuglog to show the error just as example
             
              Debug.log(newobject.getcomponent<BBB>().name);
            *****where error null reference exception occur
       }
}

so, i have come to conclusion that the error are caused by lags in code execution because the error

are not constant. sometimes after 10 loops, sometimes 11, sometimes 7 etc and also

tried the exact code (if i may, the whole game scripts) on both version of unity, only the 2019 gives the error

my questions now are;

  • is there some kind of settings in 2019 versions that might have caused this? or

  • if its a bug, is there anyway to fix

thank you:)

Code execution speed does not have anything to do with errors being or not being thrown unless you do threading stuff, which you don’t. Unless you’re using Unity’s jobs system or explicitly use threads or tasks (and you do neither in that code), Unity’s execution of your code is strictly single-threaded, which means that one line of code is executed after the next.

Of course, there’s some exceptions to that exact wording, like pauses in coroutine code or Unity’s async methods, but - and that’s the summary - if you have a regular foreach loop in a regular method, whatever your error is, it’s got nothing to do with speed. The order in which certain things happen might be undefined (like which object gets Start() called first when they’re both in the scene from the beginning), but the code still runs single-threaded. So there is no other code racing your code for faster completion. That’s simply not how regular code (or properly written multi-threaded code) works.

So when you have an issue like this, you have to find the mistake in the code, not fiddle with code execution speed.

And regarding that, I honestly can’t find anything out of the ordinary in your code that might cause irregular exception throws. So I’d recommend building a minimal example script that doesn’t do much except the stuff that you observe the bad behavior in. Try to narrow the code down to what actually causes the error and fix it.

You have to set up code execution correctly, if you get an error something is null ref in start then you need to to fill that reference in an awake call. If you set things up right you won’t get these errors.
EDIT:
Hey sorry it sounded like a race condition issue, but after reading the comments and checking through the code I see that is not the case, I’m not sure what is the issue, but wouldn’t it be easier to just have a reference to the scriptableObject?