Search Unity

Question Photon PUN 2.0 Problem

Discussion in 'Scripting' started by IngeniusPhoenix, Jan 24, 2023.

  1. IngeniusPhoenix

    IngeniusPhoenix

    Joined:
    Jun 1, 2021
    Posts:
    4
    Hello! I am using 2022.1.2.1f1 version of unity.

    I am currently following a tutorial from BlackThornProd:


    For some reason, a method is not being called.

    Here are the scripts:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6. using UnityEngine.UI;
    7.  
    8. public class LobbyManager : MonoBehaviourPunCallbacks
    9. {
    10.     public InputField roomInputField;
    11.     public GameObject lobbyPanel;
    12.     public GameObject roomPanel;
    13.     public Text roomName;
    14.  
    15.     public RoomName roomNamePrefab;
    16.     public Transform contentObject;
    17.     List<RoomName> roomNamesList = new List<RoomName>();
    18.  
    19.     public float timeBetweenUpdates = 1.5f;
    20.     float nextUpdateTime;
    21.  
    22.     public List<PlayerItem> playerItemsList = new List<PlayerItem>();
    23.     public PlayerItem playerItemPrefab;
    24.     public Transform playerItemParent;
    25.  
    26.     private void Start()
    27.     {
    28.         PhotonNetwork.JoinLobby();
    29.     }
    30.  
    31.     public void OnClickCreate()
    32.     {
    33.         if (roomInputField.text.Length >= 1 && roomInputField.text.Length <= 10)
    34.         {
    35.             PhotonNetwork.CreateRoom(roomInputField.text);
    36.         }
    37.     }
    38.  
    39.     public override void OnJoinedRoom()
    40.     {
    41.         base.OnJoinedRoom();
    42.  
    43.         lobbyPanel.SetActive(false);
    44.         roomPanel.SetActive(true);
    45.         roomName.text = PhotonNetwork.CurrentRoom.Name;
    46.  
    47.         UpdatePlayerList();
    48.     }
    49.  
    50.     public override void OnRoomListUpdate(List<RoomInfo> roomList)
    51.     {
    52.         base.OnRoomListUpdate(roomList);
    53.  
    54.         if (Time.time >= nextUpdateTime)
    55.         {
    56.             UpdateRoomList(roomList);
    57.             nextUpdateTime = Time.time + timeBetweenUpdates;
    58.         }
    59.     }
    60.  
    61.     void UpdateRoomList(List<RoomInfo> list)
    62.     {
    63.         foreach (RoomName name in roomNamesList)
    64.         {
    65.             Destroy(name.gameObject);
    66.         }
    67.  
    68.         roomNamesList.Clear();
    69.  
    70.         foreach(RoomInfo room in list)
    71.         {
    72.             RoomName newRoom = Instantiate(roomNamePrefab, contentObject);
    73.             newRoom.SetRoomName(room.Name);
    74.  
    75.             roomNamesList.Add(newRoom);
    76.         }
    77.     }
    78.  
    79.     public void JoinRoom(string roomName)
    80.     {
    81.         PhotonNetwork.JoinRoom(roomName);
    82.     }
    83.  
    84.     public void OnClickLeaveRoom()
    85.     {
    86.         PhotonNetwork.LeaveRoom();
    87.     }
    88.  
    89.     public override void OnLeftRoom()
    90.     {
    91.         base.OnLeftRoom();
    92.  
    93.         roomPanel.SetActive(false);
    94.         lobbyPanel.SetActive(true);
    95.     }
    96.  
    97.     public override void OnConnectedToMaster()
    98.     {
    99.         base.OnConnectedToMaster();
    100.  
    101.         PhotonNetwork.JoinLobby();
    102.     }
    103.  
    104.     public void UpdatePlayerList()
    105.     {
    106.         foreach (PlayerItem item in playerItemsList)
    107.         {
    108.             Destroy(item.gameObject);
    109.         }
    110.  
    111.         playerItemsList.Clear();
    112.  
    113.         if (PhotonNetwork.CurrentRoom == null)
    114.         {
    115.             return;
    116.         }
    117.  
    118.         foreach (KeyValuePair<int, Player> player in PhotonNetwork.CurrentRoom.Players)
    119.         {
    120.             PlayerItem newplayerItem = Instantiate(playerItemPrefab, playerItemParent);
    121.             newplayerItem.SetPlayerInfo(player.Value);
    122.  
    123.             if (player.Value == PhotonNetwork.LocalPlayer)
    124.             {
    125.                 newplayerItem.ApplyLocalChanges();
    126.             }
    127.  
    128.             playerItemsList.Add(newplayerItem);
    129.         }
    130.     }
    131.  
    132.     public override void OnPlayerEnteredRoom(Player newPlayer)
    133.     {
    134.         base.OnPlayerEnteredRoom(newPlayer);
    135.  
    136.         UpdatePlayerList();
    137.     }
    138.  
    139.     public override void OnPlayerLeftRoom(Player otherPlayer)
    140.     {
    141.         base.OnPlayerLeftRoom(otherPlayer);
    142.  
    143.         UpdatePlayerList();
    144.     }
    145. }
    and:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using Photon.Pun;
    6. using Photon.Realtime;
    7.  
    8. public class PlayerItem : MonoBehaviour
    9. {
    10.     public Text playerName;
    11.     public Color highlightColor;
    12.     public GameObject leftArrowButton;
    13.     public GameObject rightArrowButton;
    14.     private Image backgroundImage;
    15.  
    16.     private void Start()
    17.     {
    18.         backgroundImage = GetComponent<Image>();
    19.     }
    20.  
    21.     public void SetPlayerInfo(Player _player)
    22.     {
    23.         playerName.text = _player.NickName;
    24.     }
    25.  
    26.     public void ApplyLocalChanges()
    27.     {
    28.         backgroundImage.color = highlightColor;
    29.  
    30.         leftArrowButton.SetActive(true);
    31.         rightArrowButton.SetActive(true);
    32.     }
    33. }
    34.  
    ApplyLocalChanges(), for whatever reason, is not being called. I am a unity beginner programmer, and I have no idea how Photon works either. Thanks for the help.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Photon has their own support forum, have you tried there first?
     
  3. IngeniusPhoenix

    IngeniusPhoenix

    Joined:
    Jun 1, 2021
    Posts:
    4
    I tried posting in the Photon forums, but have not yet received a reply, so I decided to post in here.
     
    MelvMay likes this.