GameObject.Find not working on Player in DontDestroyOnLoad

499 views Asked by At

The Player Object with script GamePlayerManager is set up by the NetworkLobbyManager when creating the GameScene after Lobby using

var conn = RoomPlayers[i].connectionToClient;
var gameplayerInstance = Instantiate(gamePlayerPrefab);
gameplayerInstance.SetDisplayName(RoomPlayers[i].DisplayName);
gameplayerInstance.SetPosition(RoomPlayers[i].Position);
NetworkServer.Destroy(conn.identity.gameObject);
NetworkServer.ReplacePlayerForConnection(conn, gameplayerInstance.gameObject);

In OnStartClient on the GamePlayerManager i try to Reference a GameObjects with myHand = GameObject.Find("myHand"); but myHand is null afterwards. My Hierarchy in this moment: Hieracrchy

Doesn't it work because the Player is in DontDestroyOnLoad? And I heard that .Find isn't the best option to find reference, but what is?

1

There are 1 answers

0
Simon Trakis On

Now i found that OnStartClient is called to early for the scene to exist, used the Find methodes right before the transform change and it worked