Search Unity

2 players, 1 canvas. Why can't I do this?

Discussion in 'Multiplayer' started by shrdlu518, Mar 24, 2022.

  1. shrdlu518

    shrdlu518

    Joined:
    Mar 27, 2020
    Posts:
    6
    I created a fairly simple 2D rummy-like card game. One player against 3 bots.
    I'm trying to make it multiplayer.
    I cloned a Netcode Lobby app and added my canvas to the main scene. I can connect a host and one or more clients to the same game, but all players get their own copy of the canvas. I want each player to see the same canvas, eventually with different views of the tabletop and different cards in their hands.

    I probably don't need Player objects at all, but seeing two or more player prefabs on the same plane confirms that both players are in the same "world".

    I suspect the canvas just needs a simple setting change, but nothing was worked yet. I added Network Object and Network Transform components, changed MonoBehavior to NetworkBehavior, and put the canvas inside the NetworkManager

    In case it helps, here are the first few lines of a script on the canvas:
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEditor;
    using System;
    using System.Text.RegularExpressions;
    using Unity.Netcode;

    public class DeckOfCards : NetworkBehaviour //MonoBehaviour
    {
    #region variables
    public Text MessageText;
    public Text[] OppMessageText = new Text[4];
    public Text ScoreText;
    public CardInDeck thisCard;
    ...

    Many thanks to anyone who can tell me anything.
     
  2. devandlamadhubabu35

    devandlamadhubabu35

    Joined:
    Apr 15, 2022
    Posts:
    1
  3. Mia_white

    Mia_white

    Joined:
    Apr 15, 2022
    Posts:
    13
  4. shrdlu518

    shrdlu518

    Joined:
    Mar 27, 2020
    Posts:
    6
    The rummy game image looks like a good solution, but I don't see any link to the code (or to a tutorial or Github repository?)

    My original post may have been too general. Here's a question that I hope will have a specific answer:
    If there is an instance of the app for each player, is there one instance of the canvas, or does each player have a canvas that they populate from network data?
     
  5. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    The latter is correct, each client will have its on canvas. The server spawns the network objects onto the client, then the client decides how to display those objects on the canvas. You can have pre-existing network objects already in the scene.

    I should add though I only use canvases for UI elements, not game elements.

    Typically games are designed to be multiplayer from the ground up, so trying to retrofit multiplayer onto a single player game may not be straight forward.
     
    Last edited: Apr 19, 2022