Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Oct 07, 2017 at 03:16 PM · missingreferenceexceptionphysics.overlapsphere

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;
         }
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Oct 08, 2017 at 04:31 PM 0
Share

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)

avatar image $$anonymous$$ · Oct 09, 2017 at 11:34 PM 0
Share

I still need help. Bump

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to access destroyed object 1 Answer

Why does my Bullet prefab go missing from my gun script when destroyed? 1 Answer

Texture2D variable causes MissingReferenceException 0 Answers

,MissingReferenceException: The object of type 'Transform' 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. 0 Answers

Physics.OverlapSphere retunr 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges