instantiate players with different data PUN2 Unity

The whole answer can be found below.

Please note: 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! And we offer you support through 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.

instantiate players with different data PUN2 Unity

Starik
2022-07-31 15:08:46

Hi everyone.

I create matchmaking with PUN2 in Unity2d. I have a room for two players, which I spawn across each other. For players I have the prefab. This prefab is an object with panels for equipment. In the game scene I fill these panels by script. So I have a problem with filling spawned players.

Two ways, that I tried:

  • Fill player after instantiating. If I use condition myPhotonView.IsMine -> spawning only "my" player in the game scene. If without this condition -> two players are spawned, BUT only my player has his panels filled.

  • Fill player before. In this way I get the same results by number of instances of players on the game scene. But they all have the same filled panels.

So I want only that user sees his player and the player of the opponent with correctly filling panels. ))

There is my code...

public class GameController : MonoBehaviourPunCallbacks

{

private PhotonView myPhotonView;

private GameObject player;

[SerializeField] GameObject playerPref;

private EquipmentSlot[] equipmentSlots;

private GameDataManager gdm = new GameDataManager(); //load data from PlayerPrefs

[SerializeField] InventoryItemDatabase inventory;

void Start()

{

myPhotonView = GetComponent();

// if (myPhotonView.IsMine) {

player = PhotonNetwork.Instantiate(playerPref.name, Vector3.zero, 0);

initializeComponents(); // first way

// }

}

public void initializeComponents()

{

fillPlayer = player.GetComponent();

equipmentSlots = fillPlayer.GetEquipmentSlots();

fillEquip();

}

public void fillEquip()

{

List listOfEuipment = gdm.LoadEquipPanelEquip();

foreach (int index in listOfEuipment)

{

InventoryItem item = inventory.getInventoryItem(index);

AddEquipPanelItem(item,);

}

}

public bool AddEquipPanelItem(InventoryItem item)

{

for (int i = 0; i < equipmentSlots.Length; i++)

{

if (equipmentSlots[i].EquipmentType == item.equipmentType)

{

equipmentSlots[i].Item = item;

return true;

}

}

return false;

}

}

Thanks everyone for the help ;)

Comments

Alejandrazo
2022-07-31 18:30:14

Hey , hi again Starik

If i understood well, ¿your second option spawn all of your player slots in all the cameras of all players ? This is because are instantiate with photon i supose, try to Script everything that should dissapear, with that

If(!photonView.isMine)    
{    
Destroy(gameObject);    
}    

Starik
2022-07-31 19:43:35

Alejandrazo 2022-07-31T18:30:14+00:00

Hey , hi again Starik

If i understood well, ¿your second option spawn all of your player slots in all the cameras of all players ? This is because are instantiate with photon i supose, try to Script everything that should dissapear, with that

If(!photonView.isMine)

{

Destroy(gameObject);

}

Thanks for help, but it doesn't work ))) - on the scene is staying only one instance (player).

Maybe I'm not correctly explaining))

Now I have 2 instances (players) on the scene, if I'm not using condition. But data (equipment) is the same.

Attaching screenshot.

Снимок экрана 2022-07-31 в 22.39.56.png

Uploaded 2022-07-31T19:42:46+00:00 470851 bytes I think, that i need to do with data for these panels. Like PhotonNetwork.RaiseEvent.

Because it is not a simple image or color - with these properties all is good and works. But with like custom components, I think there needs to be a special solution ))))

Alejandrazo
2022-07-31 21:36:12

If you only want be able to see second player equipment while you are player one continue reading:


The player 1

PhotonNetwork.Instantiate(slot1Equipment,slot1position,Quaternion.identity);

The player 2

PhotonNetwork.Instantiate(slot1Equipment,slot5position,Quaternion.identity);

/slot1Equipment== the item you want for fill an slot(needs photonview for show to other players)/

/Slot1position==The position of slot 1 wich (Slot 1needs photonview)/

/Slot5position==The position of "slot1 of second player"(Slot 5 needs photonview)/


This is an example, N° of slots maybe no correct for you, i hope it works if i understood problem i hope you understand me too.

Starik
2022-07-31 22:13:11

Alejandrazo 2022-07-31T21:36:12+00:00

If you only want be able to see second player equipment while you are player one continue reading:

____________________

The player 1

PhotonNetwork.Instantiate(slot1Equipment,slot1position,Quaternion.identity);

The player 2

PhotonNetwork.Instantiate(slot1Equipment,slot5position,Quaternion.identity);

/*slot1Equipment== the item you want for fill an slot(needs photonview for show to other players)*/

/*Slot1position==The position of slot 1 wich (Slot 1needs photonview)*/

/*Slot5position==The position of "slot1 of second player"(Slot 5 needs photonview)*/

________________

This is an example, N° of slots maybe no correct for you, i hope it works if i understood problem i hope you understand me too.

So it does not work for some reasons... )) For example, if I change my prefab to the easy 2d object and change color in this object - all is good.

 void Start()    
  {    
      myPhotonView = GetComponent<PhotonView>();    
      player = PhotonNetwork.Instantiate(playerPref.name, Vector3.zero, 0);    
      player.GetComponent<SpriteRenderer>().color = Color.blue;    
}     

After this code I will get 2 objects on the scene and only "my" will have a blue color.

Unfortunately, it is not working in my case with fill players panels.

Alejandrazo
2022-08-03 04:39:48

i think you should give a number to all slots of equipment, for example:


if(photoView.isMasterClient)    
{    
fillslot1;    
fillslot2;    
fillslot3;    
}    
else    
{    
fillslot4;    
fillslot5;    
fillslot6;    
}    

in 2 players game, should not be there any problem with that i think, the equipment needs photonview too!

this easy way maybe not the best, but its a good idea i think, you need to do two groups of slots, player 1 slots and player 2 slots by any way.

Tobias
2022-08-03 10:21:08

Unsolicited tip:

When you select any text block in a post, there is a "paragraph" icon on the left (outside the input box). Click it, then the "quotation marks" icon (tooltip "Toggle Special Formats Menu") and you can turn any part into a code block.

Sorry for the convoluted way to make this happen.

Back to top