Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Player join system not working

Discussion in 'Scripting' started by DaRealXDev, Mar 12, 2021.

  1. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    Hello,
    I have created a local multiplayer join system which has worked fine for a few days but now the players don't get added to my players list but the objects are created. Can anyone help?
    Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4. using TMPro;
    5. public class GameManager : MonoBehaviour {
    6.     public List<LocalPlayer> players;
    7.     public static bool gameInProgress = false;
    8.     public Canvas[] player1UI;
    9.     public Canvas[] player2UI;
    10.     public Canvas joinMenu;
    11.     public TMP_Text[] readyTexts;
    12.     public LayerMask playerJumpLayer = 8;
    13.     void OnPlayerConnect(int playerNumber)
    14.     {
    15.         if (gameInProgress) return;
    16.         GameObject obj = new GameObject("Player",typeof(LocalPlayer));
    17.         LocalPlayer localPlayer = obj.GetComponent(typeof(LocalPlayer)) as LocalPlayer;
    18.         if (players.Count == 0) localPlayer.playerNumber = 1;
    19.         if (players.Count > 0) localPlayer.playerNumber = 2;
    20.         players.Insert(playerNumber,localPlayer);
    21.         Canvas toDisable = null;
    22.         Canvas toEnable = null;
    23.         if (localPlayer.playerNumber == 1)
    24.         {
    25.             toDisable = player1UI[0];
    26.             toEnable = player1UI[1];
    27.         }
    28.         else if (localPlayer.playerNumber == 2)
    29.         {
    30.             toDisable = player2UI[0];
    31.             toEnable = player2UI[1];
    32.         }
    33.         toDisable.enabled = false;
    34.         toEnable.enabled = true;
    35.     }
    36.     void OnPlayerDisconnect(int playerIndex)
    37.     {
    38.         if (gameInProgress) return;
    39.         GameObject player = players[playerIndex].gameObject;
    40.         LocalPlayer localPlayer = players[playerIndex];
    41.         players.Remove(localPlayer);
    42.         Destroy(player);
    43.         Canvas toDisable = null;
    44.         Canvas toEnable = null;
    45.         if (localPlayer.playerNumber == 1)
    46.         {
    47.             toEnable = player1UI[0];
    48.             toDisable = player1UI[1];
    49.         }
    50.         else if (localPlayer.playerNumber == 2)
    51.         {
    52.             toEnable = player2UI[0];
    53.             toDisable = player2UI[1];
    54.         }
    55.         toDisable.enabled = false;
    56.         toEnable.enabled = true;
    57.     }
    58.     void OnGameStart()
    59.     {
    60.         gameInProgress = true;
    61.         joinMenu.enabled = false;
    62.     }
    63.     void OnGameEnd()
    64.     {
    65.         gameInProgress = false;
    66.         joinMenu.enabled = true;
    67.     }
    68.     private void Update() {
    69.         if (Input.GetKeyDown(KeyCode.Space) && players.Count == 0)
    70.         {
    71.             OnPlayerConnect(0);
    72.         }
    73.         if (Input.GetKeyDown(KeyCode.JoystickButton7) && players.Count == 1)
    74.         {
    75.             OnPlayerConnect(1);
    76.         }
    77.         if (Input.GetKeyDown(KeyCode.Escape) && players.Count > 0)
    78.         {
    79.             OnPlayerDisconnect(0);
    80.         }
    81.         if (Input.GetKeyDown(KeyCode.JoystickButton1) && players.Count == 2)
    82.         {
    83.             OnPlayerDisconnect(1);
    84.         }
    85.         int readyPlayers = 0;
    86.         foreach (LocalPlayer v in players)
    87.         {
    88.             if (v.ready)
    89.             {
    90.                 readyPlayers += 1;
    91.             }
    92.         }
    93.         if (readyPlayers == 2)
    94.         {
    95.             OnGameStart();
    96.         }
    97.         if (gameInProgress)
    98.         {
    99.             //I'll run some code here
    100.         }
    101.     }
    102. }
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I'm not in a state to work though the logic you posted, but one thing I see missing a a healthy dose of Debug.Log()'s, start outputting everything to the console to figure out what is actually happening, as apposed to what you think is happening, work through it like a cpu and not as a human.
     
  3. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    I used debug.logs and everything seems fine, I just removed them for this forum post.