Hi. I’m making my first game on unity and I have just run into this error. Whenever I run my game I recieve this error: “NullReferenceException: Object reference not set to an instance of an object
Enemy_AI.SetDestination (UnityEngine.Vector3 dest) (at Assets/scripts/Enemy_AI.cs:82)
Enemy_AI.Chasing () (at Assets/scripts/Enemy_AI.cs:41)
Enemy_AI.Update () (at Assets/scripts/Enemy_AI.cs:36)”.
This is the script I wrote. It is an enemy AI script for an active ragdoll:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Enemy_AI : MonoBehaviour
{
public Transform player;
public LayerMask ground, isplayer;
Vector3 walkpoint;
bool walkpointset;
public float walkpointRange;
public float timebetweenattack;
bool attacked;
public float sightRange, attackRange;
bool insight, playerinattackrange;
public ConfigurableJoint spinejoint;
Animator anim;
// Start is called before the first frame update
void Awake()
{}
// Update is called once per frame
void Update()
{
insight = Physics.CheckSphere(transform.position, sightRange, isplayer);
playerinattackrange = Physics.CheckSphere(transform.position, attackRange, isplayer);
if (!insight && !playerinattackrange) Patrolling();
if (insight && !playerinattackrange) Chasing();
}
void Chasing()
{
SetDestination(player.position);
}
void Patrolling()
{
if (!walkpointset) SearchWalkPoint();
if (walkpointset)
{
SetDestination(walkpoint);
}
Vector3 distanceToWalkPoint = transform.position - walkpoint;
if (distanceToWalkPoint.magnitude < 1f)
{
walkpointset = false;
anim.SetBool("walking", false);
}
}
void SearchWalkPoint()
{
float randomz = Random.Range(-walkpointRange, walkpointRange);
float randomx = Random.Range(-walkpointRange, walkpointRange);
walkpoint = new Vector3(transform.position.x + randomx,transform.position.y, transform.position.z + randomz);
if (Physics.Raycast(walkpoint, -transform.up, ground))
{
walkpointset = true;
}
}
void SetDestination(Vector3 dest)
{
Vector3 direction = dest - transform.position;
float angle = Mathf.Atan2(direction.z, direction.x) * Mathf.Rad2Deg;
spinejoint.targetRotation = Quaternion.Euler(0f, angle, 0f);
anim.SetBool("walking", true);
}
}
Either serialise the field (make it public or add the SerializeField attribute) and assign it in the inspector, or use GetComponent in Awake/Start to assign a value to it.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:
drag it in using the inspector
code inside this script initializes it
some OTHER external code initializes it
? something else?
This is the kind of mindset and thinking process you need to bring to this problem:
I am having the same issue with null, sos…
“NullReferenceException: Obiect reference not set to an instance of an obiect
movement.ProcessMouseRotation () (at Assets/Scripts/movement.cs:45)”
I am making the player object in fps game move with mouse movement, but I believe its not in the code yet ill share it in hopes to get urgent help…