hi guys, first of all let me say that your course so far i awesome, Im really enjoying it.
for some reason unity is constantly throwing my null reference with my path finding, i understand what the null reference is and where its coming from, what im scratching my head is with how to solve it. also even though i get the null exception the path finding is still working, my guards move as the should and it doesn’t crash the game.
here is my AI controler script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Combat;
using RPG.Core;
using RPG.Movement;
using RPG.Control;
using System;
namespace RPG.Control
{
public class AI_controler : MonoBehaviour
{
[SerializeField] float chase_radius = 5f;
[SerializeField] float suspicion_time=10f;
[SerializeField] float waypoint_dwell_time = 1f;
[SerializeField] Patrol_path patrol_path;
[SerializeField] float waypoint_tolerance = 2f;
Fighter _fighter;
GameObject player;
Health health;
Vector3 guard_position;
Move move;
float time_since_saw_player = Mathf.Infinity;
float time_since_arrived_watpoint = Mathf.Infinity;
int current_waypoint_index = 0;
private void Start()
{
_fighter = GetComponent<Fighter>();
health = GetComponent<Health>();
player = GameObject.FindWithTag("Player");
guard_position = transform.position;
move = GetComponent<Move>();
}
private void Update()
{
if (health.Is_Dead()) return;
if (In_attack_range() && _fighter.Can_Attack(player))
{
Attack_behaviour();
}
else if (time_since_saw_player < suspicion_time)
{
Suspicios_behaviour();
}
else
{
Patrol_Behaviour();
}
//Debug.Log(gameObject.name + " suspicion_time= " + suspicion_time);
//Debug.Log(gameObject.name + " time_since_saw_player = " + time_since_saw_player);
Time_update();
}
private void Time_update()
{
time_since_saw_player += Time.deltaTime;
time_since_arrived_watpoint += Time.deltaTime;
}
private void Patrol_Behaviour()
{
//Vector3 next_position = guard_position;
Vector3 next_position = Get_current_waypoint();
if (patrol_path != null)
{
if (At_waypoint())
{
time_since_arrived_watpoint = 0;
Cycle_waypoint();
}
next_position = Get_current_waypoint();
}
if (time_since_arrived_watpoint > waypoint_dwell_time)
{
move.Start_movement(next_position);
}
}
private bool At_waypoint()
{
float distane_to_waypoint = Vector3.Distance(transform.position, Get_current_waypoint());
return distane_to_waypoint < waypoint_tolerance;
}
private void Cycle_waypoint()
{
current_waypoint_index = patrol_path.Get_next_waypoint(current_waypoint_index);
}
private Vector3 Get_current_waypoint()
{
Debug.Log(patrol_path.Get_waypoint(current_waypoint_index));
return patrol_path.Get_waypoint(current_waypoint_index); //line were unity null reference happens
}
private void Suspicios_behaviour()
{
GetComponent<Action_Scheduler>().Cancel_current_action();
}
private void Attack_behaviour()
{
time_since_saw_player = 0;
_fighter.Attack(player);
}
private bool In_attack_range()
{
float distane_to_player = Vector3.Distance(player.transform.position, transform.position);
return distane_to_player < chase_radius;
}
//called by unity
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, chase_radius);
}
}
}
and here is my patrol path script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Control
{
public class Patrol_path : MonoBehaviour
{
const float waypoint_gizmo_radius = 0.3f;
private void OnDrawGizmos()
{
for (int i = 0; i < transform.childCount; i++)
{
int j = Get_next_waypoint(i);
Gizmos.color = Color.red;
Gizmos.DrawSphere(Get_waypoint(i), waypoint_gizmo_radius);
Gizmos.DrawLine(Get_waypoint(i), Get_waypoint(j));
}
}
public int Get_next_waypoint(int i)
{
if (i+1 >= transform.childCount)
{
return 0;
}
return i + 1;
}
public Vector3 Get_waypoint(int i)
{
return transform.GetChild(i).position;
}
}
}