- Home /
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.
If I'm not mistaken, a basic rule is that it is safe to reference your own components in the Awake() and reference other game objects in the Start(). Don't know if that applies here, but would be worth a shot.
As much as I know all GameObjects are already initialized when Awake() gets called. I still tried, without success. The thing is, that the "Ball" GameObject is found whilst the "Game" GameObject is not.
Answer by szimmermann · Sep 26, 2016 at 10:03 AM
Are you sure the game objects tagged with "Ball" and "Game" are active? FindGameObjectWithTag only returns active objects.
Thanks for your reply! The objects are active in the scene and the tags are set correctly. The crazy thing is that the reference seems to work when I host the game with the Unity editor. If you have an idea what could possibly be wrong I'd be glad to hear it. Until then I will try to build a workaround with a coroutine that keeps repeating until the reference is set correctly.
Answer by edo_m18 · Sep 28, 2016 at 01:48 PM
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.
Hy edo_m18, The "Ball" and "Game" GameObjects are not controlled by the Network$$anonymous$$anager. Ins$$anonymous$$d they already exist in the Scene I load. Your tipp will come in hand though later in my project. Thanks.