Hi there!
I have a problem.
When I’m clicking to move the player, it appears an error saying: “: Object reference not set to an instance of an object” and it also says it is at line 36, wich is this part : “Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);”.
Here is my code:
using System.Collections.Generic;
using Mirror;
using UnityEngine;
using UnityEngine.AI;
public class PlayerMovement : NetworkBehaviour
{
[SerializeField] private NavMeshAgent agent = null;
private Camera mainCamera;
#region Server
[Command]
private void CmdMove(Vector3 position)
{
if (!NavMesh.SamplePosition(position, out NavMeshHit hit, 1f, NavMesh.AllAreas)) { return; }
agent.SetDestination(hit.position);
}
#endregion
#region Client
public override void OnStartAuthority()
{
mainCamera = Camera.main;
}
[ClientCallback]
private void Update()
{
if (!hasAuthority) { return; }
if (!Input.GetMouseButtonDown(1)) { return; }
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity)) { return; }
CmdMove(hit.point);
}
#endregion
}```