- Home /
Question by
gaeloow · Nov 22, 2018 at 11:20 PM ·
errormovementerror messagerts
RTS/MOBA movement script error,Error in RTS/moba movement script
Hi, I am having an error with a RTS/MOBA movement script. Here is the scipt:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Movement : MonoBehaviour {
Camera cam;
public LayerMask Ground;
public NavMeshAgent playerAgent;
void Awake () {
cam = Camera.main;
}
void Update () {
if (Input.GetMouseButtonDown(1))
{
playerAgent.SetDestination(GetPointUnderCursor());
}
}
private Vector3 GetPointUnderCursor()
{
Vector2 screenPosition = Input.mousePosition;
Vector3 mouseWorldPosition = cam.ScreenToWorldPoint(screenPosition);
RaycastHit hitPosition;
Physics.Raycast(mouseWorldPosition, cam.transform.forward, out hitPosition, 100000, Ground);
return hitPosition.point;
}
}
there is the error:
NullReferenceException: Object reference not set to an instance of an object Movement.GetPointUnderCursor () (at Assets/Scripts/Movement.cs:32) Movement.Update () (at Assets/Scripts/Movement.cs:24)
Could someone help me?
Comment