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 /
  • Help Room /
avatar image
0
Question by Anonynmousfluff · Apr 12, 2016 at 03:53 PM · c#unity 5index

IndexOutOfRangeExeption! This doesent even affect my gameplay, it just is there to be annoying!

using UnityEngine; using System.Collections;

public class Patrol : MonoBehaviour { public Transform[] patrolPoints; public float moveSpeed;

 private int currentPoint;

 // Use this for initialization
 void Start () {
     transform.position = patrolPoints[0].position;
     currentPoint = 0;
 

}

 // Update is called once per frame
 void Update () {
     if(currentPoint >= patrolPoints.Length)
     {
         currentPoint = 0;
     }


     if (transform.position == patrolPoints[currentPoint].position)
     {
         currentPoint++;
     }


// This is where the error occurs!

     transform.position = Vector3.MoveTowards(transform.position, patrolPoints[currentPoint].position, moveSpeed * Time.deltaTime);
 }

}

Comment
Add comment · Show 4
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 gjf · Apr 11, 2016 at 10:34 PM 0
Share

try changing

 if(currentPoint >= patrolPoints.Length)

to

 if(currentPoint > patrolPoints.Length)

arrays are zero based...

avatar image Anonynmousfluff · Apr 11, 2016 at 10:53 PM 0
Share

If I change if(currentPoint >= patrolPoints.Length) to if(currentPoint > patrolPoints.Length), the cube in my game will go forward but will no longer go back to its starting position.

avatar image gjf Anonynmousfluff · Apr 12, 2016 at 12:41 AM 0
Share

my typo... change it to

 if(currentPoint > patrolPoints.Length - 1)

avatar image Anonynmousfluff gjf · Apr 12, 2016 at 07:28 PM 0
Share

It will return to its starting position now, but I still get the error.

1 Reply

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

Answer by RybiconZX · Apr 12, 2016 at 08:45 PM

I would switch your if statements, you want to increment your index counter and then check to see if it is going out of bounds in that order. Because you have the if statements in that order the MoveTowards function has a chance to be indexing an out of bounds array element.

 //Increment first
 if(transform.position == patrolPoints[currentPoint].position) {
     currentPoint++;
 }
 
 //Check for out of bounds
 if(currentPoint >= patrolPoints.Length) {
     currentPoint = 0;
 }

also gjf advice to change the if statement from currentPoint >= patrolPoints.Length is incorrect, you had it correct originally.

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 Anonynmousfluff · Apr 13, 2016 at 08:01 PM 0
Share

Thank you so much! This works wonderfully!

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

92 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 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

[SOLVED]Removing gameobject from list don't change the index 1 Answer

IsGrounded not updating properly 1 Answer

problems with mopub 0 Answers

It does not work removing items from the inventory 0 Answers

how can i control scale speed ? 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