Cannot connect Client

I have created a Client and Server Script following the manual and a tutorial. When just attaching the scripts to objects everything works fine, but when making the unity editor a host (by creating server as well as client) and opening a build doing the same but this time only a client, it just doesn’t connect. I’m using the LoopbackIpv4 address (localhost).
Here is my Code:
BaseClient.cs

```csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Networking.Transport;
using Unity.Collections;

public class BaseClient : MonoBehaviour
{
public NetworkDriver driver;
protected NetworkConnection serverConnection;

private string ipAddr = "127.0.0.1";
private ushort port = 44444;

public ushort GetPort() { return port; }

public void SetPort(ushort port) { this.port = port; }

public string GetIpAddr() { return ipAddr; }
public void SetIpAddr(string IpAddr) { this.ipAddr = IpAddr; }

#if UNITY_EDITOR
private void Start() { Init(); }
private void Update() { UpdateServer(); }
private void OnDestroy() { Shutdown(); }
#endif

public virtual void Init()
{
    driver = NetworkDriver.Create();
    serverConnection = default;

    NetworkEndpoint endPoint = NetworkEndpoint.LoopbackIpv4.WithPort(44444);

    serverConnection = driver.Connect(endPoint);
}

public virtual void Shutdown()
{
    driver.Dispose();
}

public virtual void UpdateServer()
{
    driver.ScheduleUpdate().Complete();
    UpdateMessagePump();
    IsConnectionActive();
}

private void IsConnectionActive()
{
    if (!serverConnection.IsCreated)
    {
        Debug.Log("Lost connection");
    }
}

protected virtual void UpdateMessagePump()
{
    DataStreamReader stream;
   
    NetworkEvent.Type cmd;

    while ((cmd = serverConnection.PopEvent(driver, out stream)) != NetworkEvent.Type.Empty)
    {
        if (cmd == NetworkEvent.Type.Connect)
        {
            Debug.Log("Client connected to server");
        }
        else if (cmd == NetworkEvent.Type.Data)
        {
            OnDataReceived(stream);
        }
        else if (cmd == NetworkEvent.Type.Disconnect)
        {
            serverConnection = default(NetworkConnection);
            Debug.Log("Client disconnected");
        }
    }
}

public virtual void OnDataReceived(DataStreamReader stream)
{
    NetPacket packet = null;
    var packetType = (PacketType)stream.ReadByte();

    packet = NetUtilities.Instance.CreatePacket(stream, packetType);

    packet.ReceivedOnClient();
}

public virtual void SendPacketToServer(NetPacket packet)
{
    DataStreamWriter writer;
    driver.BeginSend(serverConnection, out writer);
    packet.Serialize(ref writer);
    driver.EndSend(writer);
}

}*
* *BaseServer.cs* *csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Networking.Transport;
using Unity.Collections;

public class BaseServer : MonoBehaviour
{
public NetworkDriver driver;
protected NativeList connections;

[SerializeField] private bool isOpenForNewPlayers = true;

public bool IsOpenForNewPlayers() { return isOpenForNewPlayers; }

public void SetIsOpenForNewPlayers(bool isOpen) { isOpenForNewPlayers = isOpen; }

#if UNITY_EDITOR
private void Start() { Init(); }
private void Update() { UpdateServer(); }
private void OnDestroy() { Shutdown(); }
#endif

public virtual void Init()
{
    driver = NetworkDriver.Create();

    connections = new NativeList<NetworkConnection>(4, Allocator.Persistent);

    NetworkEndpoint endPoint = NetworkEndpoint.AnyIpv4.WithPort(44444);

    if (driver.Bind(endPoint) != 0)
        Debug.Log("Could not bind to Port " + endPoint.Port);
    else
        driver.Listen();
}

public virtual void Shutdown()
{
    driver.Dispose();
    connections.Dispose();
}

public virtual void UpdateServer()
{
    driver.ScheduleUpdate().Complete();
    CleanUpConnection();
    //if (IsOpenForNewPlayers())
    AcceptNewConnections();
    UpdateMessagePump();
}

private void CleanUpConnection()
{
    for (int i = 0; i < connections.Length; i++)
    {
        if (!connections[i].IsCreated)
        {
            connections.RemoveAtSwapBack(i);
            i--;
        }
    }
}

private void AcceptNewConnections()
{
    NetworkConnection connection;

    while((connection = driver.Accept()) != default)
    {
        Debug.Log("Added new connection");
        connections.Add(connection);
    }
}

protected virtual void UpdateMessagePump()
{
    DataStreamReader stream;

    for(int i = 0; i < connections.Length; i++)
    {
        NetworkEvent.Type cmd;

        while ((cmd = driver.PopEventForConnection(connections[i], out stream)) != NetworkEvent.Type.Empty)
        {
            if (cmd == NetworkEvent.Type.Data)
            {
                OnDataReceived(stream);
            }
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                connections[i] = default(NetworkConnection);
                Debug.Log("Client disconnect");
            }
        }
    }
}

public virtual void OnDataReceived(DataStreamReader stream)
{
    NetPacket packet = null;
    var packetType = (PacketType)stream.ReadByte();

    packet = NetUtilities.Instance.CreatePacket(stream, packetType);

    packet.ReceivedOnServer(this);
}

public virtual void BroadcastPacket(NetPacket packet)
{
    for (int i = 0; i < connections.Length; i++)
    {
        if (connections[i].IsCreated)
            SendPacketToClient(connections[i], packet);
    }
}

public virtual void SendPacketToClient(NetworkConnection connection, NetPacket packet)
{
    DataStreamWriter writer;
    driver.BeginSend(connection, out writer);
    packet.Serialize(ref writer);
    driver.EndSend(writer);
}

}*
```

I am using Unity 2022.3.18f1, Unity Transport v.2.1.0.
Thanks in advance!

Based on the code you posted, your client will only start and update the driver if UNITY_EDITOR is defined. That’s not the case in player builds, which would explain why your client won’t connect.

Oh ok, I was following the tutorial and didn’t even listen what he said. Thanks I’m going to test it straight away!

Works great! Thanks again!

Quick off topic question: im trying to achive this:

public class ClassA : BaseClass
{
    public int val = 1;
    public int override GetValue()
    {
         return val;
    }
}

public class ClassB : BaseClass
{
    public bool val = true;
    public bool override GetValue()
    {
         return val;
    }
}

public class BaseClass
{
    public virtual [RETURN TYPE] GetValue()
    {
       return val
    }
}

I have a List with multiple BaseClasses and I want to get the Value from the subclass without needing to make one for every variable type, is this possible?

You’d want either an interface that specifies GetValue in that case or have BaseClass be abstract (if you don’t need to instances of it). I’d suggest asking general C# questions like this on Stack Overflow, as you’re likely to get more and better answers there.