This error keeps getting spammed in the console:
(Filename: C:/Users/Finalbi/Documents/Stocks and Shares/Assets/Scripts/GameManager.cs Line: 360)
NullReferenceException: Object reference not set to an instance of an object
at GameManager.Gameover (PlayerScore[] scores) [0x0002d] in C:\Users\Finalbi\Documents\Stocks and Shares\Assets\Scripts\GameManager.cs:360
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <605bf8b31fcb444b85176da963870aa7>:0
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00083] in <605bf8b31fcb444b85176da963870aa7>:0
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <605bf8b31fcb444b85176da963870aa7>:0
at Photon.Pun.PhotonNetwork.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, Photon.Realtime.Player sender) [0x005d6] in C:\Users\Finalbi\Documents\Stocks and Shares\Assets\Photon\PhotonUnityNetworking\Code\PhotonNetworkPart.cs:554
at Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) [0x000ac] in C:\Users\Finalbi\Documents\Stocks and Shares\Assets\Photon\PhotonUnityNetworking\Code\PhotonNetworkPart.cs:2202
at Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) [0x0052e] in C:\Users\Finalbi\Documents\Stocks and Shares\Assets\Photon\PhotonRealtime\Code\LoadBalancingClient.cs:3362
at ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) [0x002e4] in D:\Dev\Work\photon-dotnet-sdk\PhotonDotNet\PeerBase.cs:904
at ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () [0x003d2] in D:\Dev\Work\photon-dotnet-sdk\PhotonDotNet\EnetPeer.cs:577
at ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () [0x00038] in D:\Dev\Work\photon-dotnet-sdk\PhotonDotNet\PhotonPeer.cs:1771
at Photon.Pun.PhotonHandler.Dispatch () [0x00030] in C:\Users\Finalbi\Documents\Stocks and Shares\Assets\Photon\PhotonUnityNetworking\Code\PhotonHandler.cs:232
Rethrow as AggregateException: Caught 5 exception(s) in methods called by DispatchIncomingCommands(). Rethrowing first only (see above). (Exception has been thrown by the target of an invocation.)
at Photon.Pun.PhotonHandler.Dispatch () [0x00098] in C:\Users\Finalbi\Documents\Stocks and Shares\Assets\Photon\PhotonUnityNetworking\Code\PhotonHandler.cs:250
at Photon.Pun.PhotonHandler.FixedUpdate () [0x00012] in C:\Users\Finalbi\Documents\Stocks and Shares\Assets\Photon\PhotonUnityNetworking\Code\PhotonHandler.cs:152
This is my gameManager script:
using ExitGames.Client.Photon;
using Photon.Pun;
using Hashtable = ExitGames.Client.Photon.Hashtable;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using TMPro;
using UnityEngine;
using System.Threading.Tasks;
using System;
using System.Text;
using UnityEngine.SceneManagement;
using System.Linq;
public class GameManager : MonoBehaviourPunCallbacks
{
public GameObject Main;
public GameObject c1;
public GameObject c2;
public GameObject c3;
public GameObject c4;
public GameObject c5;
public GameObject c6;
public TextMeshProUGUI ctext1;
public TextMeshProUGUI ctext2;
public TextMeshProUGUI ctext3;
public TextMeshProUGUI ctext4;
public TextMeshProUGUI ctext5;
public TextMeshProUGUI ctext6;
public TextMeshProUGUI cdtext1;
public TextMeshProUGUI cdtext2;
public TextMeshProUGUI cdtext3;
public TextMeshProUGUI cdtext4;
public TextMeshProUGUI cdtext5;
public TextMeshProUGUI cdtext6;
public TextMeshProUGUI MoneyIndcater;
public TextMeshProUGUI TimeIndcater;
public TextMeshProUGUI StocksIndcater1;
public TextMeshProUGUI StocksIndacter2;
public TextMeshProUGUI StocksIndacter3;
public TextMeshProUGUI StocksIndacter4;
public TextMeshProUGUI StocksIndacter5;
public TextMeshProUGUI StocksIndacter6;
public TextMeshProUGUI PriceIndacter1;
public TextMeshProUGUI PriceIndacter2;
public TextMeshProUGUI PriceIndacter3;
public TextMeshProUGUI PriceIndacter4;
public TextMeshProUGUI PriceIndacter5;
public TextMeshProUGUI PriceIndacter6;
public GameObject BankruptIndacter1;
public GameObject BankruptIndacter2;
public GameObject BankruptIndacter3;
public GameObject BankruptIndacter4;
public GameObject BankruptIndacter5;
public GameObject BankruptIndacter6;
public Company company1;
public Company company2;
public Company company3;
public Company company4;
public Company company5;
public Company company6;
public int maxDays = 10;
public int days = 1;
public Dictionary<int, int> Stocks;
public List<Company> CompanysInUse;
public bool lastDay = false;
public bool gameOver = false;
public bool sentScore = false;
public bool greenLight = false;
private TaskCompletionSource<bool> greenLightTaskCompletionSource;
public int forwardVotes = 0;
public List<Player> playersVoted;
public TextMeshProUGUI voteText;
public List<PlayerScore> playersScores;
PhotonView view;
public int money;
public GameObject leaderboard;
public TextMeshProUGUI leaderboardtext;
public GameObject fowardTimeButton;
// Start is called before the first frame update
void Start()
{
sentScore = false;
gameOver = false;
lastDay = false;
view = PhotonView.Get(this);
playersScores = new List<PlayerScore>();
playersVoted = new List<Player>();
money = 10;
days = 1;
forwardVotes = 0;
greenLightTaskCompletionSource = new TaskCompletionSource<bool>();
Stocks = new Dictionary<int, int>();
Stocks[1] = 0;
Stocks[2] = 0;
Stocks[3] = 0;
Stocks[4] = 0;
Stocks[5] = 0;
Stocks[6] = 0;
voteText.text = forwardVotes + "/" + PhotonNetwork.CurrentRoom.PlayerCount;
CompanysInUse = new List<Company>();
Companies companies1 = new Companies();
if (PhotonNetwork.IsMasterClient)
{
for (int i = 0; i < 6; i++)
{
int random = UnityEngine.Random.Range(1, companies1.companies.Count);
Company company = companies1.companies[random];
company.price = UnityEngine.Random.Range(1, 10);
CompanysInUse.Add(company);
}
company1 = CompanysInUse[0];
company2 = CompanysInUse[1];
company3 = CompanysInUse[2];
company4 = CompanysInUse[3];
company5 = CompanysInUse[4];
company6 = CompanysInUse[5];
view.RPC("SetCompanysClient", RpcTarget.All, company1, company2, company3, company4, company5, company6);
}
}
private void Awake()
{
PhotonPeer.RegisterType(typeof(Company), (byte)'A', Company.Serialize, Company.Deserialize);
PhotonPeer.RegisterType(typeof(PlayerScore), (byte)'B', PlayerScore.Serialize, PlayerScore.Deserialize);
}
// Update is called once per frame
void Update()
{
MoneyIndcater.text = "Money: " + money;
TimeIndcater.text = "Days: " + days;
StocksIndcater1.text = "Stocks: " + Stocks[1];
StocksIndacter2.text = "Stocks: " + Stocks[2];
StocksIndacter3.text = "Stocks: " + Stocks[6];
StocksIndacter4.text = "Stocks: " + Stocks[4];
StocksIndacter5.text = "Stocks: " + Stocks[5];
StocksIndacter6.text = "Stocks: " + Stocks[6];
PriceIndacter1.text = "Price: " + company1.price;
PriceIndacter2.text = "Price: " + company2.price;
PriceIndacter3.text = "Price: " + company3.price;
PriceIndacter4.text = "Price: " + company4.price;
PriceIndacter5.text = "Price: " + company5.price;
PriceIndacter6.text = "Price: " + company6.price;
BankruptIndacter1.SetActive(company1.bankrupt);
BankruptIndacter2.SetActive(company2.bankrupt);
BankruptIndacter3.SetActive(company3.bankrupt);
BankruptIndacter4.SetActive(company4.bankrupt);
BankruptIndacter5.SetActive(company5.bankrupt);
BankruptIndacter6.SetActive(company6.bankrupt);
voteText.text = forwardVotes + "/" + PhotonNetwork.CurrentRoom.PlayerCount;
if (forwardVotes >= PhotonNetwork.CurrentRoom.PlayerCount && PhotonNetwork.IsMasterClient)
{
view.RPC("FowardTime", RpcTarget.All);
}
if (gameOver && !sentScore)
{
sentScore = true;
view.RPC("SendScore", RpcTarget.MasterClient, PhotonNetwork.LocalPlayer, money);
}
if (playersScores.Count >= PhotonNetwork.CurrentRoom.PlayerCount && PhotonNetwork.IsMasterClient && gameOver)
{
playersScores.Sort(new PlayerScoreComparer());
view.RPC("Gameover", RpcTarget.All, playersScores.ToArray() as object);
}
}
public void onPressBuy(int CompanyNumber)
{
int UCompanyNumber = CompanyNumber + 1;
Company companyBuyingFrom = null;
switch (UCompanyNumber)
{
case 1: companyBuyingFrom = company1; break;
case 2: companyBuyingFrom = company2; break;
case 3: companyBuyingFrom = company3; break;
case 4: companyBuyingFrom = company4; break;
case 5: companyBuyingFrom = company5; break;
case 6: companyBuyingFrom = company6; break;
}
if (companyBuyingFrom.price <= money && !companyBuyingFrom.bankrupt && !lastDay)
{
switch (UCompanyNumber)
{
case 1: money = money - company1.price; break;
case 2: money = money - company2.price; break;
case 3: money = money - company3.price; break;
case 4: money = money - company4.price; break;
case 5: money = money - company5.price; break;
case 6: money = money - company6.price; break;
}
Stocks[CompanyNumber + 1]++;
}
}
public void onPressSell(int CompanyNumber)
{
int UCompanyNumber = CompanyNumber + 1;
if (Stocks[CompanyNumber + 1] >= 1)
{
Stocks[CompanyNumber + 1]--;
switch (UCompanyNumber)
{
case 1: money = money + company1.price; break;
case 2: money = money + company2.price; break;
case 3: money = money + company3.price; break;
case 4: money = money + company4.price; break;
case 5: money = money + company5.price; break;
case 6: money = money + company6.price; break;
}
}
}
public void OnPressForwardTime()
{
view.RPC("VoteForwardTime", RpcTarget.MasterClient, PhotonNetwork.LocalPlayer);
}
[PunRPC]
public void FowardTime()
{
forwardVotes = 0;
playersVoted.Clear();
if (PhotonNetwork.IsMasterClient)
{
if (days == 1)
{
foreach (Company company in CompanysInUse)
{
int increaseAmount = UnityEngine.Random.Range(0, 5);
company.price += increaseAmount;
}
}
else
{
foreach (Company company in CompanysInUse)
{
if (!company.bankrupt)
{
int increaseAmount = UnityEngine.Random.Range(-10, 10);
company.price += increaseAmount;
company.bankrupt = company.price <= 0;
if (company.bankrupt)
{
company.price = 0;
}
}
}
}
company1 = CompanysInUse[0];
company2 = CompanysInUse[1];
company3 = CompanysInUse[2];
company4 = CompanysInUse[3];
company5 = CompanysInUse[4];
company6 = CompanysInUse[5];
view.RPC("SetCompanysClient", RpcTarget.All, company1, company2, company3, company4, company5, company6);
}
days++;
if (days == maxDays)
{
lastDay = true;
}
if (days > maxDays)
{
gameOver = true;
}
}
[PunRPC]
public void SetCompanysClient(Company company1, Company company2, Company company3, Company company4, Company company5, Company company6)
{
this.company1 = company1;
this.company2 = company2;
this.company3 = company3;
this.company4 = company4;
this.company5 = company5;
this.company6 = company6;
ctext1.text = company1.name;
ctext2.text = company2.name;
ctext3.text = company3.name;
ctext4.text = company4.name;
ctext5.text = company5.name;
ctext6.text = company6.name;
cdtext1.text = company1.description;
cdtext2.text = company2.description;
cdtext3.text = company3.description;
cdtext4.text = company4.description;
cdtext5.text = company5.description;
cdtext6.text = company6.description;
}
[PunRPC]
public void VoteForwardTime(Player player)
{
if (playersVoted.Contains(player))
{
forwardVotes--;
playersVoted.Remove(player);
return;
}
forwardVotes++;
playersVoted.Add(player);
view.RPC("sentVotes", RpcTarget.All, forwardVotes);
}
[PunRPC]
public void SendScore(Player player, int score)
{
PlayerScore playerscore = new PlayerScore(score, player);
playersScores.Add(playerscore);
}
[PunRPC]
public void Gameover(PlayerScore[] scores)
{
playersScores = scores.ToList();
StringBuilder leaderboardStringBuilder = new StringBuilder();
int postion = 1;
foreach (PlayerScore score in playersScores)
{
leaderboardStringBuilder.Append(postion + ": " + score.player.NickName + ": " + score.score + "\n");
postion++;
}
leaderboardtext.text = leaderboardStringBuilder.ToString();
MoneyIndcater.gameObject.SetActive(false);
TimeIndcater.gameObject.SetActive(false);
voteText.gameObject.SetActive(false);
fowardTimeButton.SetActive(false);
Main.SetActive(false);
c1.SetActive(false);
c2.SetActive(false);
c3.SetActive(false);
c4.SetActive(false);
c5.SetActive(false);
c6.SetActive(false);
leaderboard.SetActive(true);
}
[PunRPC]
public void sentVotes(int fowardVotes)
{
forwardVotes = fowardVotes;
}
public void OnClickBack()
{
PhotonNetwork.LeaveRoom();
SceneManager.LoadScene("Lobby");
}
public void Back()
{
c1.SetActive(false);
c2.SetActive(false);
c3.SetActive(false);
c4.SetActive(false);
c5.SetActive(false);
c6.SetActive(false);
Main.SetActive(true);
}
public void swapToC1()
{
Main.SetActive(false);
c1.SetActive(true);
}
public void swapToC2()
{
Main.SetActive(false);
c2.SetActive(true);
}
public void swapToC3()
{
Main.SetActive(false);
c3.SetActive(true);
}
public void swapToC4()
{
Main.SetActive(false);
c4.SetActive(true);
}
public void swapToC5()
{
Main.SetActive(false);
c5.SetActive(true);
}
public void swapToC6()
{
Main.SetActive(false);
c6.SetActive(true);
}
}
Line 360 is: leaderboardStringBuilder.Append(postion + ": " + score.player.NickName + ": " + score.score + "\n");
In the gameover rpc I can't understand this error and why it would be caused by this line this error is triggerd when the game ends and gameover rpc is called
I havent tried anything except trying to decypher the error.
Here is the playerScore class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Realtime;
using UnityEngine.SocialPlatforms.Impl;
using Photon.Pun;
using System.Security.Cryptography;
public class PlayerScore : IPunObservable
{
public int score;
public Player player;
public PlayerScore(int score, Player player)
{
this.score = score;
this.player = player;
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(player.ActorNumber);
stream.SendNext(score);
}
else if (stream.IsReading)
{
player = PhotonNetwork.CurrentRoom.GetPlayer((int)stream.ReceiveNext());
score = (int)stream.ReceiveNext();
}
}
public static byte[] Serialize(object customType)
{
PlayerScore playerScore = (PlayerScore)customType;
return new byte[] { (byte)playerScore.player.ActorNumber, (byte)playerScore.score };
}
public static object Deserialize(byte[] data)
{
return new PlayerScore(data[0], PhotonNetwork.CurrentRoom.GetPlayer(data[1]));
}
}
public class PlayerScoreComparer : IComparer<PlayerScore>
{
public int Compare(PlayerScore x, PlayerScore y)
{
return x.score.CompareTo(y.score);
}
}