unexpected behaviour of OnGUI

I have a OnGUI method that should display a window when a boolean “playerConnected” is set to false.

if (!playerConnected)
  windowRect = GUI.Window (1, windowRect,  WindowFunction, "Player error!");

playerConnected is set by an external script.
This does however not work.

As a test, i set the variable as follows

Update()
{
  if (Input.GetKeyDown(KeyCode.T))
      playerConnected = !playerConnected
}

This does work, so it seems that i cannot use the OnGUI as i would like.

How can i display the window when the playerConnected variable is set from another Component?

There is no difference between setting a variable from within a component or from another component. Are you sure the code setting this variable is doing this on the correct object and are you sure this code is actually being executed?

i found the problem:
Tom, you are right, variables can be set external. In my case however, the “external” component set the variable from within its Awake() method:

ComponentB : MonoBehavior
{
  ....
   void Awake()
  {
    .....
    componentA.playerConnected = true
  }
}

It turned out that the Awake() of component A was not called yet, with the result that the setting from ComponentB got “unnoticed”.

I now use an “initialiser-object” that takes care of the order in which my objects are initialised. I think it is the only way to have a clearly defined initialisation order. The Awake() method is not usefull for this, because the order in which they are called is undetermined…