Skip to content

Commit fd55651

Browse files
committed
Fixed player color setting to use OnPlayerNumberingChanged to correctly get the players number when it is set
1 parent 763abdb commit fd55651

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

Assets/CoReality/Scripts/AvatarSystem/AvatarModule.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using ExitGames.Client.Photon;
77
using System;
88
using UnityEngine.Events;
9+
using Photon.Pun.UtilityScripts;
910

1011
namespace CoReality.Avatars
1112
{
@@ -138,6 +139,9 @@ void Awake()
138139
{
139140
Instance = this;
140141

142+
//Create PlayerNumbering gameobject
143+
new GameObject("PlayerNumberingUtil").AddComponent<Photon.Pun.UtilityScripts.PlayerNumbering>();
144+
141145
//Check for network module (Ensure this is after NetworkModule in Script Execution Order)
142146
if (!NetworkModule.Instance)
143147
throw new System.Exception("NetworkModule not present in scene? Can't start AvatarModule without it.");
@@ -180,6 +184,13 @@ private void SpawnAvatar(bool remote, int viewID = -1)
180184
);
181185
_onAvatarCreated?.Invoke(avatar);
182186
PopulateAvatarList();
187+
188+
//Set color
189+
_localAvatar.Name = _randomAdjectives[UnityEngine.Random.Range(0, _randomAdjectives.Count)] + " "
190+
+ _randomNouns[UnityEngine.Random.Range(0, _randomNouns.Count)];
191+
192+
//Add listener to player number change
193+
PlayerNumbering.OnPlayerNumberingChanged += PlayerColorChanged;
183194
}
184195
else
185196
{
@@ -199,17 +210,17 @@ private void SpawnAvatar(bool remote, int viewID = -1)
199210
//Forward property change event
200211
avatar.OnPropertyChanged.AddListener((prop, val) => { _onAvatarPropertyChanged?.Invoke(avatar, prop, val); });
201212

202-
//Set color
203-
_localAvatar.Name = _randomAdjectives[UnityEngine.Random.Range(0, _randomAdjectives.Count)] + " "
204-
+ _randomNouns[UnityEngine.Random.Range(0, _randomNouns.Count)];
205-
206-
//Set the avatar's color to the actor number
207-
int actorNum = PhotonNetwork.LocalPlayer.ActorNumber;
208-
if (actorNum >= 0) //Ensure not -1
209-
_localAvatar.Color = _randomColors[PhotonNetwork.LocalPlayer.ActorNumber];
213+
}
210214

215+
private void PlayerColorChanged()
216+
{
217+
//Set color
218+
_localAvatar.Color = _randomColors[PhotonNetwork.LocalPlayer.GetPlayerNumber()];
219+
//Remove listener
220+
PlayerNumbering.OnPlayerNumberingChanged -= PlayerColorChanged;
211221
}
212222

223+
213224
/// <summary>
214225
/// Populates the avatar list with the current avatars [0] is always local
215226
/// </summary>

Assets/CoReality/Scripts/AvatarSystem/HoloAvatar.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public string Name
5151
}
5252
}
5353

54+
[SerializeField]
5455
private Color _color;
5556

5657
/// <summary>

0 commit comments

Comments
 (0)