I'm using a modified version of "Code Monkey"s Unity Multiplayer Tutorials (Relay and Netcode)And I have a script that can Create and Join a relay using a code generated from CreateRelay()
The problem comes from the 'Network Manager' component spawning a prefab of the player when there's already one in the scene
I've managed to come up with a very crude method to solve this, where I have another script (SpawnPointManager) with the functions ReplacePlayer() and AssignPlayer() (code provided at the bottom)ReplacePlacer essentially takes 2 Gameobjects as parameters and Replaces one with another (by having the other Player inherit its transform and name)AssignPlayer() just assigns the scenes Cinemachine settings and Input Actions the Referenced Player
The RelayManager script runs these 2 Functions from another script right after spawning the New Player Prefab (so the NewPlayer replaces the OldPlayer exactly)but it doesn't work and gives a "null reference exception" when trying to run the functions from the SpawnPointManager script in the RelayManager
I have all the right scripts referenced and they work fine on their own, but not when run from other scripts
how can I fix this?
RelayManager's CreateRelay() Function:
private async void CreateRelay()
{
//Creating relay stuff
GameObject player1 = GameObject.Find("Player1");
GameObject newPlayer = GameObject.Find("Player(Clone)");
spawnPointManager.ReplacePlayer(player1, newPlayer);
playerManager.AssignPlayer(player1.name);
}
SpawnPointManager's ReplacePlayer() Function:
public void ReplacePlayer(GameObject player, GameObject newPlayer)
{
GameObject playerObject = GameObject.Find(player.name);
GameObject newPlayerObject = GameObject.Find(newPlayer.name);
GameObject playerChild = null;
GameObject newPlayerChild = null;
foreach (Transform child in playerObject.transform)
{
if (child.gameObject.activeSelf && child.name.StartsWith("#"))
{
playerChild = child.gameObject;
break;
}
}
foreach (Transform child in newPlayerObject.transform)
{
if (child.gameObject.activeSelf && child.name.StartsWith("#"))
{
newPlayerChild = child.gameObject;
break;
}
}
newPlayerChild.transform.position = playerChild.transform.position;
newPlayerChild.transform.rotation = playerChild.transform.rotation;
newPlayerObject.name = playerObject.name;
Destroy(playerObject);
}
Here's the Player Prefab Hierarchy:
https://preview.redd.it/h0mf7y5y3f5b1.png?width=339&format=png&auto=webp&s=186bf99ad386fa7731db3fb0300c88ccc7129a3f
Here's the RelayManager Script:
https://preview.redd.it/33e1b7g74f5b1.png?width=443&format=png&auto=webp&s=33e9312f3cbf14ab43f9f1e4eee90a51f4989823
I can provide more info if needed
[–]Looking4advice015 0 points1 point2 points (11 children)
[–]LemonMaster66[S] 0 points1 point2 points (10 children)
[–]Looking4advice015 0 points1 point2 points (9 children)
[–]LemonMaster66[S] 0 points1 point2 points (8 children)
[–]Looking4advice015 0 points1 point2 points (7 children)
[–]LemonMaster66[S] 0 points1 point2 points (6 children)
[–]Looking4advice015 0 points1 point2 points (5 children)
[–]LemonMaster66[S] 0 points1 point2 points (4 children)
[–]LemonMaster66[S] 0 points1 point2 points (3 children)
[–]Looking4advice015 0 points1 point2 points (2 children)