instantiate players with different data PUN2 Unity

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:

  1. 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.
  2. 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<PhotonView>();

// if (myPhotonView.IsMine) {

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

  initializeComponents(); // first way

// }

    }

  public void initializeComponents()

  {

    fillPlayer = player.GetComponent<FillPlayer>();

    equipmentSlots = fillPlayer.GetEquipmentSlots();

    fillEquip();

  }

  public void fillEquip()

  {

    List<int> 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 ;)

Answers

  • Alejandrazo
    edited August 3

    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
    Starik
    edited July 31

    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.

    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 ))))

  • 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
    Starik
    edited August 3

    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
    edited August 3

    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.

  • 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.