Search Unity

Third Party Mirror add-on for a card game.

Discussion in 'Multiplayer' started by Iceman420, Jun 22, 2022.

  1. Iceman420

    Iceman420

    Joined:
    Oct 28, 2021
    Posts:
    15
    Hi all !
    Really need your help -
    I'm creating a multiplayer card game (and using mirror for the first time)
    Right now the connectivity between the Host and the client is working (I'm doing P2P networking) - which is already a good thing haha :D
    I've been trying to fit my scripts so they would work with the Mirror add-on and I'm facing some problems- *When I press the play button some objects just disappear from the scene until I assign it to be Host/client (I suppose this shouldn’t happen).
    *The Network HUD appears in different places when running the game in the editor vs. building the game. (I don't really mind that but it is strange)
    *As you will be able to see in the screen recording my script's functionality is not working good at all and the console is yelling at me :(

    I will mention that the Prefabs I want to use with network functionality all have Network Transform+Network identity components, and I have a GameObject in the scene called Network Manager with Network Manager component and I attached to this component all the Prefabs I want to be used by it.

    I will really appreciate if someone could take a look ;)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5. using UnityEngine.UI;
    6.  
    7. public class PlayerManager : NetworkBehaviour
    8. {
    9.     public GameObject survivalCard;
    10.     public GameObject player1Hand, player2Hand;
    11.     public GameObject player1DropZone, player2DropZone;
    12.  
    13.     public List<GameObject> allMysteryCards = new List<GameObject>();
    14.    
    15.  
    16.     private int maxSize = playerHand.maxSize;
    17.  
    18.     public override void OnStartClient()
    19.     {
    20.         base.OnStartClient();
    21.  
    22.         player1Hand = GameObject.FindGameObjectWithTag("player1Hand");
    23.         player2Hand = GameObject.FindGameObjectWithTag("player2Hand");
    24.         player1DropZone = GameObject.FindGameObjectWithTag("player1DropZone");
    25.         player2DropZone= GameObject.FindGameObjectWithTag("player2DropZone");
    26.         survivalCard = GameObject.FindGameObjectWithTag("survivalCard");
    27.     }
    28.  
    29.  
    30.    [Server]
    31.  
    32.     public override void OnStartServer()
    33.     {
    34.         base.OnStartServer();
    35.     }
    36.  
    37.     [Command]
    38.     public void CmdDealMysteryCard()
    39.     {
    40.         int cardsInHand = player1Hand.transform.childCount;
    41.         if (cardsInHand < maxSize)
    42.         {
    43.             Card card = TypePick();
    44.  
    45.             if ((int)card.cardType == 0)
    46.             {
    47.                 card.GetComponent<Card>().Set(DataBase.GetRandomPowerUP());
    48.             }
    49.             if ((int)card.cardType == 1)
    50.             {
    51.                 card.GetComponent<Card>().Set(DataBase.GetRandmomEventCard());
    52.             }
    53.             if ((int)card.cardType == 2)
    54.             {
    55.                 card.GetComponent<Card>().Set(DataBase.GetRandomFailureCard());
    56.             }
    57.             card = Instantiate(card, new Vector2(0, 0), Quaternion.identity);
    58.             RpcWhereToDrawCard(card.gameObject, "Dealt");
    59.  
    60.         }
    61.     }
    62.  
    63.     [Command]
    64.     public void CmdDealSurvivalCards()
    65.     {
    66.         int cardsInHand = player1Hand.transform.childCount;
    67.         if (cardsInHand < maxSize)
    68.         {
    69.             Card card = survivalCard.GetComponent<Card>();
    70.             card.GetComponent<Card>().Set(DataBase.GetRandmomSurvivalCard());
    71.             card = Instantiate(card, new Vector2(0, 0), Quaternion.identity);
    72.             NetworkServer.Spawn(card.gameObject, connectionToClient);
    73.             RpcWhereToDrawCard(card.gameObject, "Dealt");
    74.         }
    75.     }
    76.  
    77.     [ClientRpc]
    78.     public void RpcWhereToDrawCard(GameObject card,string type)
    79.     {
    80.         if (type == "Dealt")
    81.         {
    82.             if (hasAuthority)
    83.             {
    84.                card.transform.SetParent(player1Hand.transform, false);
    85.             }
    86.             else
    87.             {
    88.                 card.transform.SetParent(player2Hand.transform, false);
    89.             }
    90.         }
    91.  
    92.     }
    93.  
    94.     public Card TypePick()
    95.     {
    96.         int index = Random.Range(0, allMysteryCards.Count);
    97.         return allMysteryCards[index].GetComponent<Card>();
    98.     }
    99.  
    100. }
    101.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class DrawSurvivalCard : NetworkBehaviour
    7. {
    8.     public PlayerManager playerManager;
    9.  
    10.     private int maxSize = playerHand.maxSize;
    11.  
    12.     public void OnClick()
    13.     {
    14.         NetworkIdentity networkIdentity = NetworkClient.connection.identity;
    15.         playerManager = networkIdentity.GetComponent<PlayerManager>();
    16.         playerManager.CmdDealSurvivalCards();
    17.        
    18.     }
    19. }
    20.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class DrawMystery : MonoBehaviour
    7. {
    8.     public PlayerManager playerManager;
    9.    
    10.     private int maxSize = playerHand.maxSize;
    11.    
    12.     public void onClick()
    13.     {
    14.         NetworkIdentity networkIdentity = NetworkClient.connection.identity;
    15.         playerManager = networkIdentity.GetComponent<PlayerManager>();
    16.         playerManager.CmdDealMysteryCard();
    17.     }
    18.    
    19. }
    20.  
     
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    My experience with mirror is null, but I will say that the second point is because of the canvas is not weill configured. Check if the anchors are in the right place, and if the canvas scaler on the canvas is set to scale with screen size, and the value is 0.5 (to scale both width and height)
    I cant see the video, so that is the only thing I can say.
     
  3. Iceman420

    Iceman420

    Joined:
    Oct 28, 2021
    Posts:
    15
    Hi, thanks for answering, Iv'e changed the value to 0.5 and the canvas was already set to scale with screen size, yet it is the same...
    About the video, that's the link :

    Any one else that might know what's the problem ?
     
  4. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Have you configured the anchors?