Search Unity

Question Player Spawn Issues

Discussion in 'Multiplayer' started by markahughes88, Sep 3, 2020.

  1. markahughes88

    markahughes88

    Joined:
    Aug 24, 2020
    Posts:
    19
    I'm having some issues with spawning my players into the scene at the start of a match.

    I'm testing in a room with two users.

    For some reason each player is spawning twice (i'm guessing this is because it's spawning a prefab for the amount of clients in the room for each player). I'm not sure why this is. I know I'm using a for loop to cycle through but I've only assigned the RPC call to the player currently being targeted in the loop.

    I also assign a style to each player including a colour, a pattern and an outfit. This seems to be being applied to each instance of the prefabs spawned for the local player, but then the networked player has nothing applied and spawns as the basic prefab.

    I've attached a photo for you to see what I mean better.

    PlayerSpawner.cs
    Code (CSharp):
    1. using Photon.Pun;
    2. using Photon.Realtime;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class PlayerSpawner : MonoBehaviour
    9. {
    10.     [SerializeField] private GameObject m_PlayerPrefab;
    11.     [SerializeField] private GameObject[] m_SpawnPoints;
    12.     private Player[] m_Players;
    13.     private PhotonView pv;
    14.     private PlayerData pd;
    15.  
    16.     void Awake()
    17.     {
    18.         // Import scripts
    19.         pd = gameObject.GetComponent<PlayerData>();
    20.  
    21.         // Define photonView
    22.         pv = GetComponent<PhotonView>();
    23.     }
    24.  
    25.     void Start()
    26.     {
    27.         m_Players = PhotonNetwork.PlayerList; // Defin players
    28.  
    29.         string[] playerStyles = {
    30.             pd.m_Player.Color1,
    31.             pd.m_Player.Color2,
    32.             pd.m_Player.Pattern,
    33.             pd.m_Player.Outfit
    34.         };
    35.  
    36.         // Loop through all players and call spawn function
    37.         for(int i = 0; i < m_Players.Length; i += 1)
    38.         {
    39.             pv.RPC(
    40.                 nameof(RPCSpawnPlayer),
    41.                 m_Players[i],
    42.                 m_SpawnPoints[i].transform.position,
    43.                 playerStyles
    44.             );
    45.         }
    46.     }
    47.  
    48.     [PunRPC]
    49.     void RPCSpawnPlayer(Vector3 spawnPos, string[] style)
    50.     {
    51.         // Spawn player prefab
    52.         GameObject playerPrefab = PhotonNetwork.Instantiate(
    53.             m_PlayerPrefab.name,
    54.             spawnPos,
    55.             Quaternion.identity
    56.         );
    57.  
    58.         UpdateStyleAssets(style, playerPrefab);
    59.     }
    60.  
    61.     void UpdateStyleAssets(string[] style, GameObject go)
    62.     {
    63.         // Define style asset names
    64.         string color1 = style[0];
    65.         string color2 = style[1];
    66.         string pattern = style[2];
    67.         string outfit = style[3];
    68.  
    69.         // Define assets
    70.         Material baseColor = (Material)Resources.Load($"Colours/{color1}", typeof(Material));
    71.         Material patternColor = (Material)Resources.Load($"Colours/{color2}", typeof(Material));
    72.         Sprite[] patternSprites = Resources.LoadAll<Sprite>($"Sprites/bibbly-bobs-patterns");
    73.         Sprite[] outfitSprites = Resources.LoadAll<Sprite>($"Sprites/bibbly-bobs-outfits");
    74.         Sprite patternSprite = null;
    75.         Sprite outfitSprite = null;
    76.  
    77.         // Find pattern sprite
    78.         for (int i = 0; i < patternSprites.Length; i += 1)
    79.         {
    80.             if (patternSprites[i].name == pattern)
    81.                 patternSprite = patternSprites[i];
    82.         }
    83.  
    84.         // Find outfit sprite
    85.         for (int i = 0; i < outfitSprites.Length; i += 1)
    86.         {
    87.             if (outfitSprites[i].name == outfit)
    88.                 outfitSprite = outfitSprites[i];
    89.         }
    90.  
    91.         // Find relevant child and update style asset
    92.         Transform t = go.transform;
    93.         for (int i = 0; i < t.childCount; i += 1)
    94.         {
    95.             if (t.GetChild(i).gameObject.name == "Base")
    96.                 t.GetChild(i).gameObject.GetComponent<Renderer>().material = baseColor;
    97.                
    98.             if (t.GetChild(i).gameObject.name == "Pattern")
    99.             {
    100.                 t.GetChild(i).gameObject.GetComponent<Renderer>().material = patternColor;
    101.                 t.GetChild(i).gameObject.GetComponent<SpriteRenderer>().sprite = patternSprite;
    102.             }
    103.  
    104.             if (t.GetChild(i).gameObject.name == "Outfit")
    105.                 t.GetChild(i).gameObject.GetComponent<SpriteRenderer>().sprite = outfitSprite;
    106.         }
    107.     }
    108. }
     

    Attached Files: