multiplayer spawn question

I’m trying to wrap my head around networking in unity, and i’ve done several tutorials, but i’ve got myself stumped now.

In my current test project i’m trying to spawn 4 players randomly at 4 different preset locations.

I spawn the objects by calling a RPC function when all 4 players are connected to the server, using Network Instantiate, but I can’t figure out how I spawn them at the correct spots.

I’m sure it’s some what simple but I’m pretty inexperienced and could use some help in how to handle this.

You should create an array with the 4 positions for spawning, and randomly assign each player one position. As soon as you assign a player its position, remove it from the array to avoid two players getting the same position.

Something like (not tested in unity):


// script
public var spawnPoints : Transform[]; // assign this in the project to 4 dummy transforms (empty gameobjects) representing your spawn points

private var playersTransform : Transform[] // these are the 4 transforms of your instantiated player objects

function SpawnPlayers() // call this to spawn your players... must have the above variables populated

{

  var spawnArray : Array = new Array(); // create the array with 4 items

  spawnArray.Add(0);

  spawnArray.Add(1);

  spawnArray.Add(2);

  spawnArray.Add(3);

  var i : int = 0;

  var r : int = 0;

  for (i=0; i<playersTransform.Length; i++) // iterate through all the players

  {

    r = Random.Range(0, spawnArray.length); // get a random number from 0 to spawnArray length

    playersTransform_.position = spawnPoints[spawnArray*].position; // assign the player to this position*_
 _*spawnArray.RemoveAt(i); // remove from the array to avoid the same index to be used*_
 _*}*_
_*}*_
_*```*_

ah, ya, if i haev my server cycle through the spawn points first once the game begins and feed that info into my Network.Instantiate it should work.

I was thinking I should spawn the objects then decide where they go. I don’t know why but when it comes to this multi-player stuff I just seem to be having alot of trouble i shouldn’t be.

just realized this doesn’t really answer my main two problems.

the first of which is that i don’t know how to do a direct rpc call to each individual player with a different transform information.

for example

if I did Network.RPC(“spawnplayer”, RPCMode.AllBuffered, spawnpoint);
they would all spawn at the same spawn point, and if i try to move them once they spawn, all the variables on each spawned player will all be the same so i’m not quite sure how to tell them where to go.

the second problem is i don’t know how to get a list of all the connected players, I tried
network.connections but that doesn’t really give me any useful information, at least i don’t think so.