我一直以多种不同的方式来解决这个问题。我在获取和设置本地自定义属性时遇到了麻烦。我正在尝试选择两个不同的团队(玩家和天使)。每个玩家都是作为旁观者开始的。一旦有足够多的人加入会议室,每个人都将被分配为玩家。然后,一定比例的玩家被选为天使。我之前进行过调整,以包含SetCustomProperties。但是,我不完全确定从这里出发。 (实际上感觉就像我在黑暗中射击。)下面的代码段。
RoundSystem.cs中的代码段:
void Update()
{
playerCount = PhotonNetwork.PlayerList.Length;
if (playerCount > 2 && enoughPlayers == false)
{
enoughPlayers = true;
StartCoroutine(GameLoop());
}
}
private IEnumerator GameLoop()
{
yield return StartCoroutine(TeamBalance());
if (playerCount > 2)
{
// StartCoroutine(GameLoop());
// Commented this because I don't want to the WIP game loop to ACTUALLY loop yet
} else
{
enoughPlayers = false;
}
}
private IEnumerator TeamBalance()
{
angelCount = Mathf.Floor(PhotonNetwork.PlayerList.Length * angelPercent);
currentAngels = angelCount;
currentPlayers = PhotonNetwork.PlayerList.Length;
foreach (var item in PhotonNetwork.PlayerList)
{
Debug.Log("User changed to Citizen.");
//SetCustomProperties of team to citiString
photonView.RPC("SetPlayerTeam", item, citiString);
}
for (int i = 0; i < angelCount;)
{
var item = PhotonNetwork.PlayerList[Random.Range(0, PhotonNetwork.PlayerList.Length)];
if (item.CustomProperties["team"].ToString().Equals(citiString))
{
Debug.Log("User changed to Angel.");
//SetCustomProperties of team to angelString
photonView.RPC("SetPlayerTeam", item, angelString);
i++;
}
}
yield return null;
}
PlayerController.cs中的代码段:
[PunRPC]
public void SetPlayerTeam(string teamString)
{
hash.Add("team", teamString);
PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
}
所有球员都成功地成为观众,但是他们的球队都没有改变。我现在继续得到的错误是
NullReferenceException:对象引用未设置为对象的实例 ...(位于Assets / Scripts / RoundSystem.cs:78)