- Home /
Mob AI not immediately finding object it is looking for, and crashing when it eats food?
So I have a mob AI script that I am using on a slime model, but the problem is that I'm using Physics.OverlapSphere to find things eligible to eat, and it is picking up the terrain for most of its objects even though the terrain is only one thing, and it takes between 1-60 seconds to replace the gameobject that it has eaten with a new target, and sometimes it will crash due to its targeted food object being eaten by another slime. Any help on how to get it to not pick up the terrain as well as not crash when the food does not exist would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI : MonoBehaviour {
public GameObject Mob;
public GameObject Cloned;
public GameObject Food;
GameObject Clone;
public string FoodName;
private float Speed;
private int Timer;
private int RandomSpeed = 1;
private float Rotation;
private int Growth;
public float Size;
private bool Growing;
private bool Adult;
private int Repoints;
private int Cap;
public int MinCap;
public int MaxCap;
private Color Colored;
private float Mass;
private bool Hungry;
private float DistanceToFoodX;
private float DistanceToFoodZ;
private float Hunger;
public float Radius;
public Collider[] Colliders;
private float DistanceToFood;
private float DistanceToNewFoodX;
private float DistanceToNewFoodZ;
public GameObject Player;
public int MoveSpeed;
private bool FoodTargeted;
public bool CanClone;
void Start (){
FoodTargeted = false;
DistanceToFood = DistanceToFoodX + DistanceToFoodZ;
Speed = 1;
Hunger = 50;
Random.InitState (67);
Growing = true;
Adult = false;
Cap = Random.Range (MinCap, MaxCap);
Mob.transform.localScale = new Vector3 (0.5f * Size, 0.5f * Size, 0.5f * Size);
Colored = new Color(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255));
}
void Update () {
Debug.Log (Hunger);
//Make a collision sphere centered on Mob
Colliders = Physics.OverlapSphere (transform.position, Radius);
foreach(Collider Col in Colliders){
if (Col.gameObject.name == "Apple"){
DistanceToNewFoodX = Mathf.Abs (Col.gameObject.transform.position.x - Mob.gameObject.transform.position.x);
DistanceToNewFoodZ = Mathf.Abs (Col.gameObject.transform.position.z - Mob.gameObject.transform.position.z);
if (DistanceToNewFoodX + DistanceToFoodZ < DistanceToFoodX + DistanceToFoodZ) {
Food = Col.gameObject;
}
}
}
//Flip Mob over when stuck
Rotation = Mob.transform.eulerAngles.x;
if (Rotation <= 350) {
if (Rotation >= 10){
Mob.transform.Rotate (Vector3.left * Rotation);
}
}
//Make Mob move towards food
Hunger += -0.01f;
if (Food != null) {
FoodTargeted = true;
}
if (GameObject.Find ("Apple") != null & FoodTargeted == true) {
DistanceToFoodX = (Food.transform.position.x - Mob.transform.position.x);
DistanceToFoodZ = (Food.transform.position.z - Mob.transform.position.z);
if (Hunger <= 90) {
Hungry = true;
} else {
Hungry = false;
}
if (Hungry == true) {
//Debug.Log (DistanceToFoodX);
if (DistanceToFoodX > 0) {
Mob.transform.Translate (Vector3.right * Speed * MoveSpeed * Time.deltaTime);
}
if (DistanceToFoodX < 0) {
Mob.transform.Translate (Vector3.left * Speed * MoveSpeed * Time.deltaTime);
}
if (DistanceToFoodZ > 0) {
Mob.transform.Translate (Vector3.forward * Speed * MoveSpeed * Time.deltaTime);
}
if (DistanceToFoodZ < 0) {
Mob.transform.Translate (Vector3.back * Speed * MoveSpeed * Time.deltaTime);
}
}
}
//Make Mob grow
if (Growing == true) {
Growth += 1;
Mob.transform.localScale += new Vector3 (0.0005f * Size, 0.0005f * Size, 0.0005f * Size);
}
if (Growth >= 1000.0){
Growth = 0;
Adult = true;
Growing = false;
Mob.transform.localScale = new Vector3 (1f * Size, 1f * Size, 1f * Size);
}
//Make Mob wander around aimlessly when not hungry
if (Hungry == false) {
if (Speed == 0) {
Mob.transform.Rotate (Vector3.up * 150 * Time.deltaTime);
}
Mob.transform.Translate (Vector3.forward * Speed * Time.deltaTime);
Mob.transform.Translate (Vector3.right * Speed * Time.deltaTime);
Timer += 1;
if (Timer >= 50) {
Timer = 0;
Speed += Random.Range (-1, 1);
}
if (Speed >= 6 | Speed <= -6) {
Speed = 0;
}
if (transform.position.y < 0) {
Mob.transform.Translate (Vector3.up * -Mob.transform.position.y);
}
}
//Make Mob able to create more of itself
if (Adult == true && Hungry == false && CanClone == true && Hunger >= 60) {
Repoints += 1;
if (Repoints >= Cap){
Clone = Instantiate (Cloned, transform.position, Quaternion.identity) as GameObject;
Cap = Random.Range (MinCap, MaxCap);
Repoints = 0;
Hunger += -20;
}
}
//Make Mob die by starvation
if (Hunger <= 0) {
DestroyObject (Mob);
}
}
//Make Mob consume Food when collided with
void OnCollisionEnter(Collision EatFood){
if (EatFood.gameObject.name == "Apple") {
DestroyObject (EatFood.gameObject);
Food = GameObject.Find (FoodName);
Hunger += 10;
FoodTargeted = false;
}
}
}
The code that is crashing it should not even be running until it checks that the Food variable is not equal to null, yet it is crashing because the Food variable is equal to null, it does not make any sense.
$$anonymous$$issingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. AI.Update () (at Assets/Scripts/AI.cs:86)
Answer by $$anonymous$$ · Oct 10, 2017 at 12:21 AM
I added a statement that randomly chooses a detected object as equal to the Food variable as long as it is named "Apple". Although this made it so if there is an apple, the slime will always target it right away, the slimes still have trouble finding the one that is closest, along with that, the slimes have really jerky and weird movement when moving towards food, and will sometimes wander off when they are attempting to go towards the food. Sometimes the game will still crash when a slime eats another slimes targeted food, but it does not happen as often now for some reason. I also realized that the multiple terrain objects it was detecting was mostly trees and bushes, which are part of the terrain.
public GameObject Cloned;
public GameObject Food;
GameObject Clone;
public string FoodName;
private float Speed;
private int Timer;
private int RandomSpeed = 1;
private float Rotation;
private int Growth;
public float Size;
private bool Growing;
private bool Adult;
private int Repoints;
private int Cap;
public int MinCap;
public int MaxCap;
private Color Colored;
private float Mass;
private bool Hungry;
private float DistanceToFoodX;
private float DistanceToFoodZ;
private float Hunger;
public float Radius;
public Collider[] Colliders;
private float DistanceToFood;
private float DistanceToNewFoodX;
private float DistanceToNewFoodZ;
public GameObject Player;
public int MoveSpeed;
private bool FoodTargeted;
public bool CanClone;
void Start (){
FoodTargeted = false;
DistanceToFood = DistanceToFoodX + DistanceToFoodZ;
Speed = 1;
Hunger = 50;
Random.InitState (67);
Growing = true;
Adult = false;
Cap = Random.Range (MinCap, MaxCap);
Mob.transform.localScale = new Vector3 (0.5f * Size, 0.5f * Size, 0.5f * Size);
Colored = new Color(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255));
}
void Update () {
Debug.Log (Hunger);
//Make a collision sphere centered on Mob
Colliders = Physics.OverlapSphere (transform.position, Radius);
foreach(Collider Col in Colliders){
if (Col.gameObject.name == "Apple"){
DistanceToNewFoodX = Mathf.Abs (Col.gameObject.transform.position.x - Mob.gameObject.transform.position.x);
DistanceToNewFoodZ = Mathf.Abs (Col.gameObject.transform.position.z - Mob.gameObject.transform.position.z);
if (Food == null){
if (DistanceToNewFoodX + DistanceToFoodZ < DistanceToFoodX + DistanceToFoodZ) {
Food = Col.gameObject;
} else {
if (Col.gameObject.name == "Apple") {
Food = Col.gameObject;
}
}
}
}
}
//Flip Mob over when stuck
Rotation = Mob.transform.eulerAngles.x;
if (Rotation <= 350) {
if (Rotation >= 10){
Mob.transform.Rotate (Vector3.left * Rotation);
}
}
//Make Mob move towards food
Hunger += -0.01f;
if (Food != null) {
FoodTargeted = true;
}
if (GameObject.Find ("Apple") != null & FoodTargeted == true) {
DistanceToFoodX = (Food.transform.position.x - Mob.transform.position.x);
DistanceToFoodZ = (Food.transform.position.z - Mob.transform.position.z);
if (Hunger <= 90) {
Hungry = true;
} else {
Hungry = false;
}
if (Hungry == true) {
Debug.Log ("Finding Food");
if (DistanceToFoodX > 0) {
Mob.transform.Translate (Vector3.right * Speed * MoveSpeed * Time.deltaTime);
}
if (DistanceToFoodX < 0) {
Mob.transform.Translate (Vector3.left * Speed * MoveSpeed * Time.deltaTime);
}
if (DistanceToFoodZ > 0) {
Mob.transform.Translate (Vector3.forward * Speed * MoveSpeed * Time.deltaTime);
}
if (DistanceToFoodZ < 0) {
Mob.transform.Translate (Vector3.back * Speed * MoveSpeed * Time.deltaTime);
}
}
}
//Make Mob grow
if (Growing == true) {
Growth += 1;
Mob.transform.localScale += new Vector3 (0.0005f * Size, 0.0005f * Size, 0.0005f * Size);
}
if (Growth >= 1000.0){
Growth = 0;
Adult = true;
Growing = false;
Mob.transform.localScale = new Vector3 (1f * Size, 1f * Size, 1f * Size);
}
//Make Mob wander around aimlessly when not hungry
if (Hungry == false | Food == null) {
if (Speed == 0) {
Mob.transform.Rotate (Vector3.up * 150 * Time.deltaTime);
}
Mob.transform.Translate (Vector3.forward * Speed * Time.deltaTime);
Mob.transform.Translate (Vector3.right * Speed * Time.deltaTime);
Timer += 1;
if (Timer >= 50) {
Timer = 0;
Speed += Random.Range (-1, 1);
}
if (Speed >= 6 | Speed <= -6) {
Speed = 0;
}
if (transform.position.y < 0) {
Mob.transform.Translate (Vector3.up * -Mob.transform.position.y);
}
}
//Make Mob able to create more of itself
if (Adult == true && Hungry == false && CanClone == true && Hunger >= 60) {
Repoints += 1;
if (Repoints >= Cap){
Clone = Instantiate (Cloned, transform.position, Quaternion.identity) as GameObject;
Cap = Random.Range (MinCap, MaxCap);
Repoints = 0;
Hunger += -20;
}
}
//Make Mob die by starvation
if (Hunger <= 0) {
DestroyObject (Mob);
}
}
//Make Mob consume Food when collided with
void OnCollisionEnter(Collision EatFood){
if (EatFood.gameObject.name == "Apple") {
DestroyObject (EatFood.gameObject);
Food = GameObject.Find (FoodName);
Hunger += 10;
FoodTargeted = false;
}
}
}