Can't reference gameObject in NetworkBehaviour.Awake()

Hello Community

I am currently developing a little multiplayer game. But I stumbled over a problem I struggle with. As you see in the code below my player loads some other gameObjects in Awake(). Now I have the following behaviour:

-Hosting from Editor: In the editor everything works fine. On the standalone build tough it can’t find the Script of the “Game”-Object and throws a NullReferenceException.
-Hosting from Standalone: Now I get both in the editor and the standalone build the NullReferenceException mentioned above.

Here the code snippet:

public class PlayerController1 : NetworkBehaviour {

public float speed;

private GameObject game;
private GameController gameScript;
private GameObject ball;

private Ray ray;
private RaycastHit hit;
private Vector3 playerStartPosition;

public override void OnStartLocalPlayer() {
    gameObject.GetComponent<Renderer>().material.color = Color.blue;
}

void Awake() {
    ball = GameObject.FindGameObjectWithTag("Ball");    // Is null when hosted on standalone
    game = GameObject.FindGameObjectWithTag("Game");    // Is null when hosted on standalone
    gameScript = game.GetComponent<GameController>();    // NullReferenceError
}

void Start() {
    // set player start position
    playerStartPosition = transform.position;

    CmdClientReady();
}


}

From debugging in the editor I know that I can’t even find the gameObjects when I host a game on the standalone build (which would explain the NullReferenceException). But as much as I know, when Awake() is called, all gameObjects should be instantiated right? Therefore I don’t understand why it can’t find the gameObjects/the script.

Thanks in advance for all responses. If you need more information, I will gladly provide it.

Sincerely,
Simon

EDIT: Just to specify this a little closer: The problem is that the second GameObject “Game” is not found in Awake(). The “Ball” GameObject is found though! I also made a fix now that solves the problem: I use a coroutine that executes until all GameObjects are referenced. I also count how many times the coroutine is executed. Now the interesting thing is, that in the Editor the coroutine is executed only ONCE, whilst in the Standalone is executed TWICE. This would explain, why it is not working in the first place, as the second GameObject “Game” seems not to be found in the Standalone right at start. What I’m still curious about is, WHY it can’t be found.

Are you sure the game objects tagged with “Ball” and “Game” are active? FindGameObjectWithTag only returns active objects.

The “Ball” and “Game” are controlled by NetworkManager?
If so, Maybe you need to use ClientScene.FindLocalObject or NetworkServer.FindLocalObject methods.

A client working on the server, to use NetworkServer.FindLocalObject method.
Or a client working on the client, to use ClientScene.FindLocalObject method.

You consider to use these methods, You should use NetworkInstanceId as an argument for methods.

Like below.

// for client
GameObject target = ClientScene.FindLocalObject(netId);

// for server
GameObject target = NetworkServer.FindLocalObject(netId);

I was wondering these problem too.
I could see these object in the scene hierarchy, but I couldn’t get them.

You need to seem other things is to get NetworkInstanceId from where.