I am having hard time to spawning players for my game, I followed Blackthornprod tutorial for multiplayer but I can't spawn players just like he do, The errors I get every time I run it is "NullReferenceException: Object reference not set to an instance of an object PlayerSpawner.Start () (at Assets/PlayerSpawner.cs:18)" and if I move the arrow to change my avatars there's new error that I see and its "DefaultPool failed to load "Player". Make sure it's in a "Resources" folder. Or use a custom IPunPrefabPool. UnityEngine.Debug:LogError (object)" and "NullReferenceException: Object reference not set to an instance of an object PlayerSpawner.Start () (at Assets/PlayerSpawner.cs:20)" idk why so here's my code for spawning players in photon 2 unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class PlayerSpawner : MonoBehaviour
{
public GameObject[] playerPrefabs;
public Transform[] spawnPoints;
public PlayerItem _playerItem;
private void Start()
{
int randomNumber = Random.Range(0, spawnPoints.Length);
Debug.Log("1");
Transform spawnPoint = spawnPoints[randomNumber];
Debug.Log("2");
GameObject playerToSpawn = playerPrefabs[(int)PhotonNetwork.LocalPlayer.CustomProperties["playerAvatar"]];
Debug.Log("3");
PhotonNetwork.Instantiate(playerToSpawn.name, spawnPoint.position, Quaternion.identity);
Debug.Log("4");
}
}
and here's the other code for players that I want to spawn
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
public class PlayerItem : MonoBehaviourPunCallbacks
{
public Text playerName;
public Color highlightColor;
public GameObject leftArrowButton;
public GameObject rightArrowButton;
ExitGames.Client.Photon.Hashtable playerProperties = new ExitGames.Client.Photon.Hashtable();
public Image playerAvatar;
public GameObject[] avatars;
Player player;
private void Start()
{
}
public void SetPlayerInfo(Player _player)
{
playerName.text = _player.NickName;
player = _player;
UpdatePlayerItem(player);
}
public void ApplyLocalChanges()
{
leftArrowButton.SetActive(true);
rightArrowButton.SetActive(true);
}
public void OnClickLeftArrow()
{
if ((int)playerProperties["playerAvatar"] == 0)
{
playerProperties["playerAvatar"] = avatars.Length - 1;
}
else
{
playerProperties["playerAvatar"] = (int)playerProperties["playerAvatar"] - 1;
}
PhotonNetwork.SetPlayerCustomProperties(playerProperties);
}
public void OnClickRightArrow()
{
if ((int)playerProperties["playerAvatar"] == avatars.Length - 1)
{
playerProperties["playerAvatar"] = 0;
}
else
{
playerProperties["playerAvatar"] = (int)playerProperties["playerAvatar"] + 1;
}
PhotonNetwork.SetPlayerCustomProperties(playerProperties);
}
public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)
{
if (player == targetPlayer)
{
UpdatePlayerItem(targetPlayer);
}
}
void UpdatePlayerItem(Player player)
{
if (player.CustomProperties.ContainsKey("playerAvatar"))
{
playerAvatar.sprite = avatars[(int)player.CustomProperties["playerAvatar"]].GetComponent<SpriteRenderer>().sprite;
playerProperties["playerAvatar"] = (int)player.CustomProperties["playerAvatar"];
}
else
{
playerProperties["playerAvatar"] = 0;
}
}
}
also last thing if you ever need this but idk if this is needed or relevant but here's the code that will deliver my player to the next scene where I need it to spawn
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
public class LobbyManager : MonoBehaviourPunCallbacks
{
public InputField roomInputField;
public GameObject lobbyPanel;
public GameObject roomPanel;
public Text roomName;
public RoomItem roomItemPrefab;
public List<RoomItem> roomItemList = new List<RoomItem>();
public Transform contentObject;
public float timeBetweenUpdates = 1.5f;
float nextUpdateTime;
public List<PlayerItem> playerItemsList = new List<PlayerItem>();
public PlayerItem playerItemPrefab;
public Transform playerItemParent;
public GameObject playButton;
private void Start()
{
PhotonNetwork.JoinLobby();
}
public void OnClickCreate()
{
if (roomInputField.text.Length >= 1)
{
PhotonNetwork.CreateRoom(roomInputField.text, new RoomOptions() { MaxPlayers = 10, BroadcastPropsChangeToAll = true });
}
}
public override void OnJoinedRoom()
{
lobbyPanel.SetActive(false);
roomPanel.SetActive(true);
roomName.text = "Room: " + PhotonNetwork.CurrentRoom.Name;
UpdatePlayerList();
}
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
if (Time.time >= nextUpdateTime)
{
UpdateRoomList(roomList);
nextUpdateTime = Time.time + timeBetweenUpdates;
}
}
void UpdateRoomList(List<RoomInfo> list)
{
foreach (RoomItem item in roomItemList)
{
Destroy(item.gameObject);
}
roomItemList.Clear();
foreach (RoomInfo room in list)
{
RoomItem newRoom = Instantiate(roomItemPrefab, contentObject);
newRoom.SetRoomName(room.Name);
roomItemList.Add(newRoom);
}
}
public void JoinRoom(string roomName)
{
PhotonNetwork.JoinRoom(roomName);
}
public void OnClickLeaveRoom()
{
PhotonNetwork.LeaveRoom();
}
public override void OnLeftRoom()
{
lobbyPanel.SetActive(true);
roomPanel.SetActive(false);
}
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinLobby();
}
void UpdatePlayerList()
{
foreach (PlayerItem item in playerItemsList)
{
Destroy(item.gameObject);
}
playerItemsList.Clear();
if (PhotonNetwork.CurrentRoom == null)
{
return;
}
foreach (KeyValuePair<int, Player> player in PhotonNetwork.CurrentRoom.Players)
{
PlayerItem newPlayerItem = Instantiate(playerItemPrefab, playerItemParent);
newPlayerItem.SetPlayerInfo(player.Value);
if (player.Value == PhotonNetwork.LocalPlayer)
{
newPlayerItem.ApplyLocalChanges();
}
playerItemsList.Add(newPlayerItem);
}
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
UpdatePlayerList();
}
public override void OnPlayerLeftRoom(Player otherPlayer)
{
UpdatePlayerList();
}
private void Update()
{
if (PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom.PlayerCount >= 1)
{
playButton.SetActive(true);
}
else
{
playButton.SetActive(false);
}
}
public void OnClickPlayButton()
{
PhotonNetwork.LoadLevel("Game");
}
}
Thank you in advance if you ever decided to help me! I will put you in Credits of my game as payment, Thank youuuuu
There are three problems.
So it will work.
Good work!