Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiplayer: Choose a prefab(GaveUp)

Discussion in 'Scripting' started by tijanikun, Jan 21, 2019.

  1. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Hello,
    I got a problem with my character select screen with matchmaking system:
    When the host choose a character it's works for any character, but when other player join that doesn't work, in host side the character spawned is the same as host and it doesn't spawn on player side:

    Looby override script:
    Code (CSharp):
    1.  
    2.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    3.     {
    4.         if (minato == true)
    5.         {
    6.  
    7.             GameObject player = (GameObject)Instantiate(MinatoPlayer, Vector3.zero, Quaternion.identity);
    8.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    9.         }
    10.         if (Saitama == true)
    11.         {
    12.             GameObject player = (GameObject)Instantiate(SaitamaPlayer, Vector3.zero, Quaternion.identity);
    13.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    14.         }
    15.         if (Goku == true)
    16.         {
    17.             GameObject player = (GameObject)Instantiate(GokuPlayer, Vector3.zero, Quaternion.identity);
    18.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    19.         }
    20.         if (War == true)
    21.         {
    22.             GameObject player = (GameObject)Instantiate(WarPlayer, Vector3.zero, Quaternion.identity);
    23.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    24.         }
    25.     }
    CharacterSelect button script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterSelectionScreen : MonoBehaviour {
    6.     public GameObject MinatoButton;
    7.     public GameObject WarButton;
    8.     public GameObject GokuButton;
    9.     public GameObject SaitamaButton;
    10.     public bool WarSelected = false;
    11.     public bool MinatoSelected = false;
    12.     public bool SaitamaSelected = false;
    13.     public bool GokuSelected = false;
    14.     // Use this for initialization
    15.  
    16.     public void Wargreymon()
    17.     {
    18.         MinatoButton.GetComponent<CharacterSelectionScreen>().MinatoSelected = false;
    19.         SaitamaButton.GetComponent<CharacterSelectionScreen>().SaitamaSelected = false;
    20.         GokuButton.GetComponent<CharacterSelectionScreen>().GokuSelected = false;
    21.         WarSelected = true;
    22.     }
    23.     public void Minato()
    24.     {
    25.         WarButton.GetComponent<CharacterSelectionScreen>().WarSelected = false;
    26.         SaitamaButton.GetComponent<CharacterSelectionScreen>().SaitamaSelected = false;
    27.         GokuButton.GetComponent<CharacterSelectionScreen>().GokuSelected = false;
    28.         MinatoSelected = true;
    29.     }
    30.    public void Goku()
    31.     {
    32.         WarButton.GetComponent<CharacterSelectionScreen>().WarSelected = false;      
    33.         MinatoButton.GetComponent<CharacterSelectionScreen>().MinatoSelected = false;
    34.         SaitamaButton.GetComponent<CharacterSelectionScreen>().SaitamaSelected = false;
    35.         GokuSelected = true;
    36.     }
    37.     public void Saitama()
    38.     {
    39.         WarButton.GetComponent<CharacterSelectionScreen>().WarSelected = false;
    40.         MinatoButton.GetComponent<CharacterSelectionScreen>().MinatoSelected = false;
    41.         GokuButton.GetComponent<CharacterSelectionScreen>().GokuSelected = false;
    42.         SaitamaSelected = true;
    43.     }
    44. }
    45.  
    ScreenShot are on inspector

    I guess it's because i don't send button bool to the host but i don't know what should i do exactly

    I Realy need help please
     

    Attached Files:

  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You could use an RPC or a Syncvar hook to synchronise the player prefabs. Personally speaking I'd probably go with the Syncvar method.

    I don't use UNET anymore so I can't give you a code example off the top of my head.

    With PUN I would probably use the PhotonInstantiate callback along with the photonInstantiationData argument to enable the correct player prefab.
     
    tijanikun likes this.
  3. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    I didn't really understood where to use the Syncvar, on bool ?
    Can someone explain please ?
    Thank you
     
  4. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Im trying to sync bool but won't that cause problem if 2 client select their prefab at same time?

    Even so i can't find a solution for multiple syncvar, i got an error with that:

    Code (CSharp):
    1.  
    2.     [SyncVar(hook = "Wargreymon")]
    3.     [SyncVar(hook = "Minato")]
    4.     [SyncVar(hook = "Goku")]
    5.     [SyncVar(hook = "Saitama")]
     
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You probably ought to change your prefab selection method to use an enum instead of a bool, then you can make that a syncvar and select the prefab based on its value.
     
  6. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    I got that:
    Code (CSharp):
    1.  public enum Selection { none, minato, saitama, goku, war };
    2.     [SyncVar] public Selection currentSelect;
    3.  
    4.  
    5. public void Minato()
    6.     {
    7.             currentSelect = Selection.minato;
    8.  
    9.     }
    and

    Code (CSharp):
    1.   public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    2.     {
    3.        
    4.         if (MinatoButton.GetComponent<CharacterSelectionScreen>().currentSelect =  ???)
    5.        
    6.         {
    7.  
    8.             GameObject player = (GameObject)Instantiate(MinatoPlayer, Vector3.zero, Quaternion.identity);
    9.            
    10.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    11. }
    12. }
    13.         }
    What should i write on "if" for override to know if the 1st script got enum minato selected please ?
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Code (CSharp):
    1. if (MinatoButton.GetComponent<CharacterSelectionScreen>().currentSelect ==  Selection.minato)
    2. {
    3.     //...
    4. }
     
  8. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Thank you for your help, it's still not fixed but i think the whole concept won't works
    I'm thinking about another script, i close this thread
     
  9. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You might want to consider using Photon PUN instead of UNET, personally I find it much easier to get5 on with (and it isn't deprecated).
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The way I handled this type of thing is I don't make the Player prefab the actual character. I instantiate/spawn that as a separate object once in the game. Then just set references between the Player object and the character object so you can use the Player object for sending Commands to the server, and controlling movement of the character object. This is also handy for swapping out character objects during the game, letting keep the same Player object for that client.

    Also, I'd walk away from Unet entirely though.