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 kurakuramaki · Dec 16, 2016 at 11:58 AM · aichase

Problem with chasing ai

the code is working pretty well at first until the AI manage to catch the 1st mouse in the array it stop chasing and it give the error that said "the object have been destroy but you still try to access it" which is weird cuz if the other object in array get destroy it still work perfectly.

here is the code

public Transform[] mouses;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {        
     Vector3 currentPos = transform.position;
     Transform tMin = null;
     float minDist = Mathf.Infinity;
     foreach (Transform mouse in mouses)
     {
         float dist = Vector3.Distance(mouse.position, currentPos);
         if (dist < minDist)
         {
             tMin = mouse;
             minDist = dist;
             transform.LookAt(tMin);
             transform.position += transform.forward * 3.5f * Time.deltaTime;
         }
         Debug.Log(tMin);
     }
 }
 private void OnTriggerEnter(Collider other)
 {
     if (other.tag == "mouse")
     {
         Destroy(other.gameObject);
     }
 }
Comment
Add comment · Show 5
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 GrKl · Dec 16, 2016 at 12:20 PM 0
Share

You should remove your destroyed mouse from your 'mouses' array when destroying the gameobject. I would recommend using a List ins$$anonymous$$d of an array

It also seems to me that in your search for the closest mouse, you should first deter$$anonymous$$e wich one is the closest (in your foreach loop) and save it as a variable, then only (after your loop) move your transform. As is, for each mouse that is closer than the previous, you'll look at it and move towards it. You should only do that to the ONE mouse that is the closest, and you cant know that before you finished your loop.

avatar image kurakuramaki GrKl · Dec 16, 2016 at 02:45 PM 0
Share

Ok I will go studies about list and see if it work.Ty sir.

avatar image Fluffy_Kaeloky · Dec 16, 2016 at 12:23 PM 0
Share

@Gr$$anonymous$$l You should promote your comment to an answer. Also, a simple condition

 if (mouse == null)

Will return true if the object has been destroyed.

avatar image kurakuramaki Fluffy_Kaeloky · Dec 16, 2016 at 02:41 PM 0
Share

so where should I add that to the line?

avatar image ericbegue · Dec 16, 2016 at 12:34 PM 0
Share
              transform.LookAt(t$$anonymous$$in);
              transform.position += transform.forward * 3.5f * Time.deltaTime;

These two lines look weird to me. I guess you want to found the closest mouse, then move to it. If that the case, these line should be outside the foreach loop.

1 Reply

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

Answer by Fluffy_Kaeloky · Dec 16, 2016 at 02:50 PM

As @GrKl said, you should use a list instead of an array, and use the condition if (mouse == null) to remove destroyed objects.

I modified your code :

     public List<Transform> mouses = new List<Transform>();
 
     void Update()
     {
         Vector3 currentPos = transform.position;
         Transform tMin = null;
         float minDist = Mathf.Infinity;
 
         List<Transform> destroyedMouses = new List<Transform>();
         foreach (Transform mouse in mouses)
         {
             if (mouse == null)
             {
                 destroyedMouses.Add(mouse);
                 continue;
             }
 
             float dist = Vector3.Distance(mouse.position, currentPos);
             if (dist < minDist)
             {
                 tMin = mouse;
                 minDist = dist;
                 transform.LookAt(tMin);
                 transform.position += transform.forward * 3.5f * Time.deltaTime;
             }
             Debug.Log(tMin);
         }
 
         foreach (Transform mouse in destroyedMouses)
             mouses.Remove(mouse);
         destroyedMouses.Clear();
     }

I added destroyedMouses list since you can't edit mouses while in the loop, you would probably crash.

Comment
Add comment · Show 1 · 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
avatar image GrKl · Dec 19, 2016 at 06:26 AM 0
Share

Humm... OP should really understand he should remove

                  transform.LookAt(t$$anonymous$$in);
                  transform.position += transform.forward * 3.5f * Time.deltaTime;

from his foreach loop. That loop should only search for the closest mouse and not do anything to them before the end of the loop.

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

7 People are following this question.

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

Related Questions

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 0 Answers

How to make the NPC following players in multiplayer game 0 Answers

Help with a chasing script 2 Answers

Enemy chase script // No longer attacking 0 Answers

Can't make enemy respawn 0 Answers


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