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 MMitchell2000 · May 12, 2017 at 12:10 PM · c#scripting problempatrol

How do I determine the previous point that my enemy was at?

In a patrolling system I have what the current patrol point is, but I'd like to know what the previous patrol point is as well to make this work:

if (currentPatrolPoint.position.y < previousPatrolPoint.position.y) { anim.SetBool("walkDown", true); downSearch.SetActive(true);

This is what I have so far:

  if (currentPatrolIndex == 0)
             {
                 previousPatrolIndex = patrolPoints.Length;
             }
             else
             {
                 previousPatrolIndex = (currentPatrolIndex - 1);
             }
 
             currentPatrolPoint = patrolPoints[currentPatrolIndex];
             previousPatrolPoint = patrolPoints[previousPatrolIndex];

But the last line is pulling up an error that says, "Array index is out of range." Help please? Thanks!

Comment
Add comment
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

1 Reply

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

Answer by Davirtuoso · May 12, 2017 at 12:37 PM

If the currentPatrolIndex was 0, this would set previousPatrolIndex to the length of your array. The problem is that array lengths start at 1 and array indexes start at zero, so where an array that has 5 indexes has a length of 5, the indexes range from 0-4.

To get around it, make your first if statement do this instead:

 previousPatrolIndex = (patrolPoints.Length - 1);

This will then make sure it goes to the last entry in the array and not the one after (which doesn't exist). I hope that helps :)

Comment
Add comment · Show 2 · 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 MMitchell2000 · May 12, 2017 at 02:11 PM 0
Share

Thank you! That fixed the error and the explanation was appreciated :)

avatar image Davirtuoso MMitchell2000 · May 14, 2017 at 09:14 AM 0
Share

Not a problem - I'm happy to be of assistance :)

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can i make my soldier to patrol over the space station in random ? 1 Answer

Unity Navmesh agent patrol and chase player script issues. 0 Answers

Restricting movement with Mathf.Clamp 4 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