I’m making a multiplayer game in Unity using Netcode for GameObjects. After implementing a camera for each player that follows them, I realised that, when the client joins, the script that allows the player to control its character simply disappears. Therefore, the player can’t move. The Camera Controller script does the same thing. I also get no errors.
There’s also a Player Network script that I usually keep disabled, because I don’t need it right now. However, when I do try to enable it, when the client spawns, the scripts are still not there, but I also get these two errors:
[Netcode] NetworkBehaviour index 3 was out of bounds for Player(Clone). NetworkBehaviours must be the same, and in the same order, between server and client.
and
[Netcode] Network variable delta message received for a non-existent behaviour. NetworkObjectId: 2, NetworkBehaviourIndex: 3
There’s some more text that follows these errors, which I believe pinpoints where the errors came from, but they’re part of some scripts or functions that are included in the Netcode package I think? Let me know if you need the full errors.
It’s probably unrelated to my first issue, but it probably shows that the PlayerNetwork script is also broken. I’m gonna be honest, I followed a tutorial when making it and I don’t actually understand much from it.
Here are the scripts:
Player:
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class Player : NetworkBehaviour
{
private Rigidbody playerRb;
public GameObject camHolderPrfb;
public GameObject camHolder;
public float speed;
public float jumpForce;
public float gravityMod;
private bool onGround = true;
void Start()
{
camHolder = Instantiate(camHolderPrfb, transform.position, Quaternion.identity);
playerRb = GetComponent<Rigidbody>();
Physics.gravity *= gravityMod;
}
void Update()
{
//Makes sure player can only control its character
if (!IsOwner) {enabled = false;}
//Calculates player movement
float forwInput = Input.GetAxis("Vertical");
float horzInput = Input.GetAxis("Horizontal");
Vector3 direction = camHolder.transform.forward * forwInput + camHolder.transform.right * horzInput;
//Applies movement to player
if (direction.magnitude > 1) {direction.Normalize();}
playerRb.AddForce(direction * speed);
//Allows player to jump
if(Input.GetKeyDown(KeyCode.Space) && onGround) {
onGround = false;
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
//Ensures player can only jump if it's touching the ground
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground")) {
onGround = true;
}
}
}
Camera Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Unity.Netcode;
public class CamController : NetworkBehaviour
{
public float rotationSpeed;
public float lerpSpeed;
public GameObject camHolder;
private Camera playerCamComp;
private GameObject playerCam;
public Player PlayerScr;
void Start()
{
camHolder = PlayerScr.camHolder;
playerCamComp = camHolder.GetComponentInChildren<Camera>();
playerCam = playerCamComp.gameObject;
}
void FixedUpdate()
{
//Camera is rotatable with left and right keys
float camInput = Input.GetAxis("MoveCam");
camHolder.transform.Rotate(Vector3.up, camInput * Time.deltaTime * rotationSpeed);
//Camera moves and looks at the player
camHolder.transform.position = Vector3.Lerp(camHolder.transform.position, transform.position, lerpSpeed * Time.deltaTime);
playerCam.transform.LookAt(this.transform);
}
}
Player Network:
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>());
Destroy(transform.GetComponent<CamController>());
}
}
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
}
The internet simply didn’t give me anything related, so the solution might not be very straightforward. Please let me know if you need any additional information, like the Network Manager configuration or stuff like that. There’s also a script that only makes buttons to join the game as a host or a client, but zi don’t think it’s relevant.