The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to spawn players in spawn positions without overlapping?

BHS
2022-11-02 11:16:23

Hello, I am trying to spawn players in the spawn positions, when my game starts the game manager instantiates the players, how can I assign the firs player to first spawn position the second to the second spawn position and so on?

in my game up to 4 players can join, and I have 4 spawn positions.

it can be played as 2 , 3 or 4 players

I tried to use this code but it doesn't work...


public GameObject playerPrefab;

public Transform[] spawnPositions;

private void Start()

{

PhotonNetwork.Instantiate(playerPrefab.name, transform.position, Quaternion.identity, 0);

AssignPlayerPositions();

}

void AssignPlayerPositions()

{

GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");

if (objs.Length == 1)

{

objs[0].transform.position = spawnPositions[0].position;

}

if (objs.Length == 2)

{

objs[0].transform.position = spawnPositions[0].position;

objs[1].transform.position = spawnPositions[1].position;

}

if (objs.Length == 3)

{

objs[0].transform.position = spawnPositions[0].position;

objs[1].transform.position = spawnPositions[1].position;

objs[2].transform.position = spawnPositions[2].position;

}

if (objs.Length == 4)

{

objs[0].transform.position = spawnPositions[0].position;

objs[1].transform.position = spawnPositions[1].position;

objs[2].transform.position = spawnPositions[2].position;

objs[3].transform.position = spawnPositions[3].position;

}

}

Comments

Tobias
2022-11-02 12:33:45

Have a look at OnJoinedInstantiate.cs. It has a list of spawn points and could be a blueprint for your case.

BHS
2022-11-03 08:13:58

Tobias 2022-11-02T12:33:45+00:00

Have a look at OnJoinedInstantiate.cs. It has a list of spawn points and could be a blueprint for your case.

ty for your reply, i tried to use the OnJoinedInstantiate.cs on my game manager, i added the prefab and spawn locations, but when the game started it didn't spawned the player... can u tell me how can i use this script?

Tobias
2022-11-04 12:08:19

when the game started it didn't spawned the player

It doesn't spawn players when the game starts. You have to connect, join a room and then it will spawn the players. This should be automatic, provided the script is in the scene and enabled, when the client actually joins the room (OnJoinedRoom() happens once and if the component is not live and registered to get callbacks, then it will miss this moment)...

Also make sure you don't spawn the objects, then load another scene (which usually destroys existing objects).

Back to top