com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

NetworkConfig.ConnectionData causes NullRefException

Open Falkonio opened this issue 4 years ago • 4 comments

I am using simple approval:

using System.Text;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;

public class NetworkPassword : MonoBehaviour
{
    [SerializeField] private InputField PasswordInputField;
    [SerializeField] private Button HostButton;
    [SerializeField] private Button ClientButton;
    [SerializeField] private Button LeaveButton;

    private void Start()
    {
        HostButton.onClick.AddListener(Host);
        ClientButton.onClick.AddListener(Client);
        LeaveButton.onClick.AddListener(Leave);
    }

    public void Host()
    {
        NetworkManager.Singleton.ConnectionApprovalCallback += ApprovalCheck;
        NetworkManager.Singleton.StartHost();
    }

    public void Client()
    {
        NetworkManager.Singleton.NetworkConfig.ConnectionData = Encoding.ASCII.GetBytes(PasswordInputField.text);
        NetworkManager.Singleton.StartClient();
    }

    public void Leave()
    {
        NetworkManager.Singleton.Shutdown();
    }

    private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback)
    {
        string password = Encoding.ASCII.GetString(connectionData);
        bool approveConnection = string.Equals(password,PasswordInputField.text);
        callback(true, null, approveConnection, null, null);
    }
}

If the client enters the correct host password, then it connects correctly, even if the password is blank. If it enters a wrong password or sends an empty one, and the host has a password, then an exception occurs:

NullReferenceException: Object reference not set to an instance of an object Unity.Netcode.NetworkManager.OnNetworkEarlyUpdate () (at Library/PackageCache/[email protected]/Runtime/Core/NetworkManager.cs:1144) Unity.Netcode.NetworkManager.NetworkUpdate (Unity.Netcode.NetworkUpdateStage updateStage) (at Library/PackageCache/[email protected]/Runtime/Core/NetworkManager.cs:1115) Unity.Netcode.NetworkUpdateLoop.RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage updateStage) (at Library/PackageCache/[email protected]/Runtime/Core/NetworkUpdateLoop.cs:149) Unity.Netcode.NetworkUpdateLoop+NetworkEarlyUpdate+<>c.<CreateLoopSystem>b__0_0 () (at Library/PackageCache/[email protected]/Runtime/Core/NetworkUpdateLoop.cs:172)

In the build, this is not noticeable and it seems that everything is correct.

Falkonio avatar Oct 27 '21 19:10 Falkonio

Sorry for the late response. Were you able to fix this (if so how?) or is it still an issue?

LukeStampfli avatar Jan 04 '22 13:01 LukeStampfli

Added to backlog MTT_3232

ashwinimurt avatar Apr 07 '22 02:04 ashwinimurt

@Falkonio can you please confirm if you're still seeing this in the latest version of the SDK?

JesseOlmer avatar Jul 22 '22 16:07 JesseOlmer

Assigning to myself to verify and close.

ashwinimurt avatar Aug 01 '22 23:08 ashwinimurt

Closing due to lack of activity or response from the reporting user.

JesseOlmer avatar Oct 10 '22 20:10 JesseOlmer