- Home /
Simple Networking Question
I have an empty game object called transformsContainer that should always contain all players. When player 1 hosts a game, I call the SpawnPlayer method:
public void SpawnPlayer(Transform spawnPoint)
// spawns a player prefab at a location described by the transform
{
GameObject ship = (GameObject)Network.Instantiate(playerPrefab,
spawnPoint.position, spawnPoint.rotation, 0);
ship.transform.parent = transformsContainer;
}
That works fine. Then player 2 joins the game and I call the above function again. On player 2's side, it's own ship that the script freshly created for player 2 is also correctly put in the transformsContainer. But the player 1 ship that is in there from the beginning of the game is not in the game object, it's directly under the root! The same goes vice versa. From player 1's perspective, the ship it created when the game was created is put in the container just fine but when player 2 joins and the SpawnPlayer function is called again for player 2's ship, it doesn't put it in the transformsContainer but under the root again. I should note that I'm not getting any compile- or runtime warnings or errors.
Can anybody explain to me what's going on? :)
Thanks ahead of time.
Probably this because transformsContainer is different on all the connected players and is it filled differently on each player over the timeline. I think better than use Network.Instantiate is to use some RPCs from connected players to the server asking the server to fill transformsContainer then servers sending RPCs to clients with the transformsContainer content. Why don't use simple lists ins$$anonymous$$d of parenting?
You're right. $$anonymous$$y mistake was that I didn't consider that only the first line of code would be run via network, there's nothing that makes the second line run on everybody's machine! So that only gets executed on the player's side! That's what I need to use an RPC for. Got it! Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613125140im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
connect client - host with tow different scenes 0 Answers
How to load the second online scene in UNET? 0 Answers
why a list of transforms is empty after loading new scene? 2 Answers
Changing the player's starting position depending on the scene transition. 1 Answer
How to move the world/scene not the player - or not? 1 Answer