I’m making a multiplayer game in Unity, and I’ve started by implementing the multiplayer feature itself. After making good progress, I get an error telling me that type or namespace UnityTransport can’t be found. After trying to solve it and making no permanent changes to my project, suddenly I can’t attach the scripts to any gameobject, because “It doesn’t include MonoBehaviour or the names don’t match”.
Here are the three scripts that are used (the other two aren’t used anywhere yet, they’re imported from another project and work perfectly fine there):
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class Player : NetworkBehaviour
{
private Rigidbody playerRb;
public float speed;
void Start()
{
playerRb = GetComponent<Rigidbody>();
}
void Update()
{
if (!IsOwner)
{
return;
}
float forwInput = Input.GetAxis("Vertical");
float horzInput = Input.GetAxis("Horizontal");
Vector3 direction = Vector3.forward * forwInput + Vector3.right * horzInput;
if (direction.magnitude > 1) { direction.Normalize(); }
MoveServerRpc(direction);
}
[ServerRpc]
void MoveServerRpc(Vector3 direction)
{
playerRb.AddForce(direction * speed);
}
}
Second:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
[System.Serializable]
public struct PlayerNetworkState : INetworkSerializable
{
public Vector3 Position;
public Vector3 Rotation;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref Position);
serializer.SerializeValue(ref Rotation);
}
}
public class PlayerNetwork : NetworkBehaviour
{
[SerializeField] private bool usingServerAuth;
private NetworkVariable<PlayerNetworkState> playerState;
private Rigidbody playerRb;
private void Awake()
{
playerRb = GetComponent<Rigidbody>();
var permission = usingServerAuth ? NetworkVariableWritePermission.Server : NetworkVariableWritePermission.Owner;
playerState = new NetworkVariable<PlayerNetworkState>(new PlayerNetworkState {Position = Vector3.zero, Rotation = Vector3.zero}, NetworkVariableReadPermission.Everyone, permission);
}
public override void OnNetworkSpawn()
{
if (!IsOwner) Destroy(transform.GetComponent<Player>());
}
private void Update()
{
if (IsOwner) TransmitState();
else ConsumeState();
}
#region Transmit State
private void TransmitState()
{
var state = new PlayerNetworkState
{
Position = playerRb.position,
Rotation = transform.rotation.eulerAngles
};
if (IsServer || !usingServerAuth)
{
playerState.Value = state;
}
else
{
TransmitStateServerRpc(state);
}
}
[ServerRpc(RequireOwnership = false)]
private void TransmitStateServerRpc(PlayerNetworkState state)
{
playerState.Value = state;
}
#endregion
#region Consume State
private void ConsumeState()
{
transform.position = playerState.Value.Position;
transform.rotation = Quaternion.Euler(playerState.Value.Rotation);
}
#endregion
}
Third:
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class NetworkButtons : MonoBehaviour
{
void OnGUI() {
GUILayout.BeginArea(new Rect(10,10,300,300));
if (!NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer) {
if (GUILayout.Button("Host")) NetworkManager.Singleton.StartHost();
if (GUILayout.Button("Server")) NetworkManager.Singleton.StartServer();
if (GUILayout.Button("Client")) NetworkManager.Singleton.StartClient();
}
GUILayout.EndArea();
}
private void OnAwake()
{
GetComponent<UnityTransport>().SetDebugSimulatorParameters(
packetDelay: 120,
packetJitter: 5,
dropRate: 3);
}
}
I’ve updated Netcode and Unity Transport packages to their latest versions (1.5.2 and 2.3.0 respectively). I did include using Unity.Netcode.Transports.UTP;. This is my first project, so my experience is basically zero, so if this is a dumb question, sorry! I just haven’t found any solutions online. If you need any other info or screenshots, I’ll be happy to provide them.
Screenshot
Bobcat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6