Same Character for different player

aledg

New member
Hi guys, I'm using PUN.
To spwaning different characters I'm using the class in the documentation like this.


C#:
using UnityEngine;
using Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Game;
using Photon.Realtime;

public class MySpawnManager : SpawnManagerBase
{
    
    protected override GameObject GetCharacterPrefab(Player newPlayer)
    {
        GameObject m_Character = Resources.Load<GameObject>(GlobalControl.MyAvatarModel);
        return m_Character;
    }

}

<< GlobalControl.MyAvatarModel is a String variables set in the prev scene.

It works and I can load different dynamic characters, but the problem is that the two users don't see different characters but they see the same characters that they have also selected for the opponent ...

1471

1472
 

Justin

Administrator
Staff member
What does GlobalControl.MyAvatarModel return? You'll need to ensure that you return a different model based on the Player parameter.
 

aledg

New member
Return a different model, in fact the player plays with the correct avatar, the problem that all other users have the same Avatar ...

For example

User1 selects blue avatar
User2 selects red avatar


User1 plays with blue avatar (but also sees User2 with blue avatar)
User2 plays with red avatar (but also sees User2 with red avatar)
 

Justin

Administrator
Staff member
How does that property know which model to select for each player? The same GetCharacterPrefab method is called for both the local and remote players so you need to ensure it is returning the correct model based on the player id. If you place a breakpoint in this method I bet that GetCharacterPrefab is returning the same object no matter what client joins.
 

aledg

New member
Thank right now it's clear, but Where I can find example or documentation to do that? I suppose that I've to binding the ID of the user to the character model and inside MySpawnManager class I've to spawn the right model
 

Justin

Administrator
Staff member
It really depends on your game implementation but it can be something as simple as:

Code:
[SerializedField] protected GameObject[] m_Characters;
...
    protected override GameObject GetCharacterPrefab(Player newPlayer)
    {
        return m_Characters[newPlayer.ActorNumber];
    }
 

NIOBruno

New member
Hello,

I'm having the same problem, did someone managed to solve this issue?
I would really appriciate if there is a solution for this problem.
 

Arachai

New member
Been scratching my head on this one for a few hours myself now; I can get to the point of loading different Player prefabs in the game editor but when I build and try to run two instances the second one does not load the selected character. I've been trying to do it with the integer of the prefab to load but can not figure out a simple solution to select a character!!
 

Arachai

New member
Ok so I have made some progress and can get the character to spawn but am getting some minor errors that I know are caused by the Instantiate method. My code so far:

Code:
public class CharacterSelection : MonoBehaviour
{
    public static CharacterSelection CS;
    public int mySelectedCharacter;
    public GameObject[] aCharacters;


    private void OnEnable()
    {
        if(CharacterSelection.CS == null)
        {
            CharacterSelection.CS = this;
        }

        else
        {
            if(CharacterSelection.CS != this)
            {
                Destroy(CharacterSelection.CS.gameObject);
                CharacterSelection.CS = this;
            }
        }
    }
    // Start is called before the first frame update
    void Start()
    {

    }

    private void Update()
    {
        if (PlayerPrefs.HasKey("MyCharacter"))
        {
            mySelectedCharacter = PlayerPrefs.GetInt("MyCharacter");
        }

        else
        {
            mySelectedCharacter = 0;
            PlayerPrefs.SetInt("MyCharacter", mySelectedCharacter);
        }
    }
}
Code:
public class CharacterSpawnManager : SpawnManagerBase
{
    [SerializeField]
    protected GameObject m_Character;
    [SerializeField]
    protected GameObject[] m_Characters;

    public GameObject Character { get { return m_Character; } set { m_Character = value; } }

    protected override GameObject GetCharacterPrefab(Player newPlayer)
    {
        m_Characters = new GameObject[CharacterSelection.CS.aCharacters.Length];

        for(int i = 0; i < CharacterSelection.CS.aCharacters.Length; i ++)
        {
            m_Characters[i] = Instantiate(CharacterSelection.CS.aCharacters[CharacterSelection.CS.mySelectedCharacter]) as GameObject;
        }

        //m_Character = CharacterSelectionManager.CSM.characterSelected;
        Debug.Log("Current Character:" + CharacterSelectionManager.CSM.characterSelected.name);
        // Return the same character for all instances.
        return m_Characters[newPlayer.ActorNumber];
        //return m_Character;
    }
}
So in My CharacterSelection script I am setting up the array and updating the selected character. I am then passing that over to the CharacterSpawnmanager. I am still newish but can figure things out in Unity scripting so I may be wrong but I think the issue lies with Instantiate.

The errors are ViewID errors: "Illegal view ID:0 method: LoadDefaultLoadoutRPC GO:"

If someone could be so kind as to give me a push in the right direction it would be greatly appreciated; I'm getting close on my own I can feel it!
 

Arachai

New member
Thank you Justin!! All I needed was that push in the right direction! For those who are looking for a solution here is the code:
Code:
public class CharacterSpawnManager : SpawnManagerBase
{
    [SerializeField]
    protected GameObject[] m_Characters;

    protected override GameObject GetCharacterPrefab(Player newPlayer)
    {
        m_Characters = new GameObject[CharacterSelection.CS.aCharacters.Length];

        for (int i = 0; i < CharacterSelection.CS.aCharacters.Length; i++)
        {
            //m_Characters[i] = CharacterSelection.CS.aCharacters[4];
            m_Characters[i] = CharacterSelection.CS.aCharacters[CharacterSelection.CS.mySelectedCharacter];
        }

        Debug.Log("My Selected Character:" + CharacterSelection.CS.mySelectedCharacter);
        // Return the same character for all instances.
        return m_Characters[newPlayer.ActorNumber];
        //return m_Character;
    }
}
 
Top