Randomize the position of the player

I would like to ask how to randomise the position of the player once the network is loaded. The code snippet below is what i have done to randomise the position of the player but it doesn’t seem to work. All the players are spawn at the same position. Can i know what wrong with the code?

function OnNetworkLoadedLevel()
    {
    	// Randomize starting location
    	var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    	Debug.Log("spawns: "+spawnpoints.length);
    	
    	var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
    	var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);
    }

If you want to spawn in a random position, try using something like this:

// having determined the spawnPoint, get a random point in a circle:
var randomCircle : Vector2 = Random.insideUnitCircle;
var spawnOffset : Vector3 = new Vector3(randomCircle.x, 0, randomCircle.y);

var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position + spawnOffset, spawnpoint.rotation, 0);

Otherwise, if you want to have a large number of spawnPoints, you can either collect them with the use of a common tag, and use the code that you posted in your question (make sure that all the transforms that you are using as spawn points have the “Spawnpoint” tag), or you can manually define them by dragging them into an array in the inspector:

// right at the top!
public var spawnPoints : Transform[];

// then when you go to use it:

var spawnPoint : Transform = spawnPoints[Random.Range(0, spawnPoints.Length)];

If you want to use both, there’s no reason why you can’t apply the position randomisation after having chosen a spawnPoint!

Random.Range() returns a float value, but you are using it as an array index (which is an int value). Maybe that’s the problem? Try rounding the output of random:

var spawnpoint : Transform = spawnpoints[Mathf.RoundToInt(Random.Range(0, spawpoints.length))].transform;

Does it work?