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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Unity Multiplayer Photon Unity Networking Problem

Discussion in 'Multiplayer' started by THEKACPERGAMES, Jun 26, 2019.

  1. THEKACPERGAMES

    THEKACPERGAMES

    Joined:
    Mar 28, 2019
    Posts:
    8
    Hello. I'm new to unity (I'm creating one of my first games :D). And i have a "problem".
    My code is:

    MenuGlowne (MainMenu)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class MenuGlowne : MonoBehaviour
    7. {
    8.  
    9.     MenadzerPolaczen mp;
    10.     public InputField ifNick;
    11.  
    12.     void Start()
    13.     {
    14.         DontDestroyOnLoad(gameObject);
    15.         mp = GetComponent<MenadzerPolaczen>();
    16.     }
    17.  
    18.     // Odpala mape gdy BtnGraj
    19.     public void BtnGraj ()
    20.     {
    21.         if (ifNick.text.Length >= 4)
    22.         {
    23.             PhotonNetwork.playerName = ifNick.text;
    24.             mp.Polacz();
    25.         }
    26.  
    27.     }
    28.  
    29. }

    Menadzer Polaczen (ConnectingMenager)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class MenadzerPolaczen : Photon.MonoBehaviour {
    7.  
    8.     void Update ()
    9.     {
    10.         if (Input.GetKeyDown(KeyCode.L))
    11.         {
    12.             Gracz.Debuguj();
    13.         }
    14.     }
    15.  
    16.     public void Polacz ()
    17.     {
    18.         PhotonNetwork.ConnectUsingSettings("EarlyAcces_June2019_ClosedAlpha_0.0.1");
    19.     }
    20.  
    21.     void OnGUI ()
    22.     {
    23.         GUI.Label(new Rect(50, 30, 200, 20), PhotonNetwork.connectionStateDetailed.ToString());
    24.     }
    25.  
    26.     void OnJoinedLobby()
    27.     {
    28.         SceneManager.LoadScene(1);
    29.     }
    30.  
    31.     void OnPhotonRandomJoinFailed()
    32.     {
    33.         PhotonNetwork.CreateRoom(null);
    34.     }
    35.  
    36.     void OnLevelWasLoaded(int level)
    37.     {
    38.         if (level != 0)
    39.         {
    40.             PhotonNetwork.JoinRandomRoom();
    41.         }
    42.     }
    43.  
    44.     void OnPhotonPlayerConnected(PhotonPlayer pp)
    45.     {
    46.         if (PhotonNetwork.isMasterClient)
    47.         {
    48.             photonView.RPC("GraczWszedl", PhotonTargets.AllBuffered, pp);
    49.         }
    50.     }
    51.  
    52.     void OnPhotonPlayerDisconnected (PhotonPlayer pp)
    53.     {
    54.  
    55.     }
    56.  
    57.     [PunRPC]
    58.     void GraczWszedl(PhotonPlayer pp)
    59.     {
    60.         Gracz gracz = new Gracz();
    61.         Gracz.gracze.Add(gracz);
    62.         gracz.nick = pp.NickName;
    63.         gracz.pp = pp;
    64.     }
    65.  
    66.     [PunRPC]
    67.     void GraczWyszedl (PhotonPlayer pp)
    68.     {
    69.  
    70.     }
    71.  
    72.     void OnCreatedRoom()
    73.     {
    74.         photonView.RPC("GraczWszedl", PhotonTargets.AllBuffered, PhotonNetwork.player);
    75.     }
    76. }

    Gracz (Player)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gracz {
    6.     public string nick = "";
    7.     public PhotonPlayer pp;
    8.  
    9.     public static List<Gracz> gracze = new List<Gracz>();
    10.  
    11.     public static void Debuguj()
    12.     {
    13.         Debug.Log("Game list debug! Player count: " + gracze.Count + "all players:: ");
    14.             foreach (var gracz in gracze)
    15.             {
    16.                 Debug.Log(gracz.nick + ", ");
    17.             }
    18.     }
    19. }
    20.  
    All is nice, but when i get down BtnGraj (Button play) I see this in consle
    https://zapodaj.net/61dabae5dd0ba.png.html


    PS: Sorry for my English

    Grasz = Player
    Wszedl = Connected
    Wyszedl = Disconnected
    BtnGraj = Button Play
     
  2. THEKACPERGAMES

    THEKACPERGAMES

    Joined:
    Mar 28, 2019
    Posts:
    8
    The error is on line 88 on MenadzerPolaczen, but i delated my comments in the script so now it’s line 74 in MenadzerPolaczen
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,991
    You could fix this by not calling the RPC "GraczWszedl". Everyone else knows when a client joins, as there is a callback for that.
    IInRoomCallbacks has OnPlayerEnteredRoom. Do the PUN Basics Tutorial.
     
  4. THEKACPERGAMES

    THEKACPERGAMES

    Joined:
    Mar 28, 2019
    Posts:
    8
    So if I just delete RPC "GraczWszedl" it will work?
     
  5. THEKACPERGAMES

    THEKACPERGAMES

    Joined:
    Mar 28, 2019
    Posts:
    8
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,991
  7. Liipson

    Liipson

    Joined:
    Apr 2, 2019
    Posts:
    1