Colin MacLeod
New member
I'm trying to change the character that is spawned based on a custom player property. The thing is, I can only get it to work on the Master client - the value for Player in my other clients always seems to be the same - the master value.
Not sure of the best way to describe this - there are quite a few steps, but here goes… (I have a copy of the whole test project which I could share with you via a Google Drive link.)
Steps to Reproduce:
I created a minimal test project to show the issue. Here's what I did:
- On Unity 2019.2.8f1, start a new project
- Import Photon "PUN 2 - FREE", Opsive "Third Person Controller" and Opsive "PUN Multiplayer Add-On for Opsive Character Controllers"
- Add these scenes to the build properties:
* Assets/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Demo/Menu.unity
* Assets/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Demo/DemoRoom.unity
- Open DemoRoom.unity. Bake lighting. Save.
- Get hold of the Kyle character and use the character manager to make a prefab.
- Set a fixed spawn location - I was having issues getting the spawn points to work
- Change the SingleCharacterSpawnManager class. Add a new custom character game object to look like this:
- Change the SingleCharacterSpawnManager class implementation for GetCharacterPrefab to this:
- Add m_CustomCharacter to SingleCharacterSpawnManagerInspector, so the OnInspectorGUI is now:
- Back in DemoRoom.unity, set the Kyle prefab to the value of m_CustomCharacter
- Open scene Menu.unity
- Add a toggle. Add the following component to the toggle
- Start up 2 clients and run the app
- Toggle the custom character on the master client but not the other
Expected Behavior:
On the master client, you control the custom character, and see the other player as the normal character.
On the other client, you control the normal character and see the other player as the custom character.
Actual Behavior:
On the master client, you control the custom character, and see the other player as the normal character.
But on the other client, both characters look the same - like the custom character
Could you please help me work this out?
Thanks!
Colin
Not sure of the best way to describe this - there are quite a few steps, but here goes… (I have a copy of the whole test project which I could share with you via a Google Drive link.)
Steps to Reproduce:
I created a minimal test project to show the issue. Here's what I did:
- On Unity 2019.2.8f1, start a new project
- Import Photon "PUN 2 - FREE", Opsive "Third Person Controller" and Opsive "PUN Multiplayer Add-On for Opsive Character Controllers"
- Add these scenes to the build properties:
* Assets/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Demo/Menu.unity
* Assets/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Demo/DemoRoom.unity
- Open DemoRoom.unity. Bake lighting. Save.
- Get hold of the Kyle character and use the character manager to make a prefab.
- Set a fixed spawn location - I was having issues getting the spawn points to work
- Change the SingleCharacterSpawnManager class. Add a new custom character game object to look like this:
C#:
[Tooltip("A reference to the character that PUN should spawn _if we want something custom_. This character must be setup using the PUN Multiplayer Manager.")]
[SerializeField] protected GameObject m_CustomCharacter;
- Change the SingleCharacterSpawnManager class implementation for GetCharacterPrefab to this:
C#:
protected override GameObject GetCharacterPrefab(Player newPlayer)
{
if (newPlayer.CustomProperties != null)
{
var character = newPlayer.CustomProperties["Character"];
if ("Custom".Equals(character))
{
Debug.Log("CUSTOM CHARACTER SELECTED");
return m_CustomCharacter;
}
}
return m_Character;
}
- Add m_CustomCharacter to SingleCharacterSpawnManagerInspector, so the OnInspectorGUI is now:
C#:
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(PropertyFromName("m_Character"));
EditorGUILayout.PropertyField(PropertyFromName("m_CustomCharacter"));
if (EditorGUI.EndChangeCheck()) {
InspectorUtility.RecordUndoDirtyObject(target, "Change Value");
serializedObject.ApplyModifiedProperties();
}
base.OnInspectorGUI();
}
- Back in DemoRoom.unity, set the Kyle prefab to the value of m_CustomCharacter
- Open scene Menu.unity
- Add a toggle. Add the following component to the toggle
C#:
using Photon.Pun;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;
using Hashtable = ExitGames.Client.Photon.Hashtable;
[RequireComponent(typeof(Toggle))]
public class CharacterSetterToggle : MonoBehaviour
{
[SerializeField] private string _defaultCharacter = "Default";
[SerializeField] private string _customCharacter = "Custom";
private Toggle _toggle;
private void Awake()
{
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable
{
{"Character", _defaultCharacter}
});
_toggle = GetComponent<Toggle>();
Assert.IsNotNull(_toggle, "I need a component of type " + nameof(Toggle));
_toggle.isOn = false;
_toggle.onValueChanged.AddListener(OnToggleChanged);
}
private void OnDestroy()
{
_toggle.onValueChanged.RemoveListener(OnToggleChanged);
}
private void OnToggleChanged(bool isOn)
{
var character = isOn ? _customCharacter : _defaultCharacter;
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable
{
{"Character", character}
});
}
}
- Start up 2 clients and run the app
- Toggle the custom character on the master client but not the other
Expected Behavior:
On the master client, you control the custom character, and see the other player as the normal character.
On the other client, you control the normal character and see the other player as the custom character.
Actual Behavior:
On the master client, you control the custom character, and see the other player as the normal character.
But on the other client, both characters look the same - like the custom character
Could you please help me work this out?
Thanks!
Colin
Last edited: