global variable doesn't work


I have a problem with my global variable… In my method doSomething(), my nbwidg = 0.
But, in my FixedUpdate, nbwidg = 5.

I don’t understand how to get this number 5 with my doSomething methode.
What is the problem, please ?

private int nbwidg;

void Start()  {
void OnDataReceived(Message message)
          if ( ... )
               nbwidg = 5;

void FixedUpdate() { ... }

void doSomething()
            for (int i = 0; i < nbwidg; i++)
                // I need (nbwidg = 5) here 

I can think of 2 cases here that might be causing this issue.

Case #1: OnDataReceived is some kind of asynchronous method, meaning that its result can come after some time has passed and doSomething has already executed by then which means that when doSomething executes nbwidg will be 0 since OnDataReceived hasn’t happened yet, but on FixedUpdate, which happens later, the correct value will already be set.

Case #2: At Start, that if check inside OnDataReceived evaluates to false, so nbwidg won’t be set. But then OnDataReceived is called again some time later and that if now evaluates to true so you get the expected value in FixedUpdate.

That’s what I can conclude about the code shown. If you show more, pinpointing the issue will be easier.

Edit: Also, just to clarify, nbwidg, is not a global variable. It is a private field of a class instance.

Thank you for your answer !
But the problem is that OnDataReceived is called before… Because I need OnDataReceived to create my scene in Unity. And I verified and I try “Debug.Log” => The condition ‘‘if’’ of my OnDataReceived is called before (evaluates to true).

It is why I dont understand, it’s very strange :frowning: