• 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 JoshMBeyer · Sep 03, 2014 at 02:04 AM · arrayoutofbounds

Why is my Array going out of bounds??

 using UnityEngine;
 using System.Collections;
 
 public class XY_MoveScript1 : MonoBehaviour
 {
   
     //Movement Parameters.
     public Transform[] points;
 
     public bool startAutomaticly;
     public bool startFromTrigger;
     public bool enableMultiPoints;
 
     public GameObject triggerObject;
     public Vector3 offset;
 
 
     public float speed;
     public float delayStartBy = 0;
     private bool go;
     private Transform destination;
    
     private bool yes;
     public bool switchIt = false;
     int currentPoint = 0;
 
     void Awake() { transform.position = points[0].position; }
    
     void Start()
     {
         //---------------Through Errors if Needed----------------
         if (!startFromTrigger && !startAutomaticly) { Debug.LogError("You have not specified to StartAutomaticly or StartFromTrigger!" + this.gameObject); return; }
         if (startAutomaticly && startFromTrigger) { Debug.LogError("You have StartFromTrigger And Start automaticly checked on " + this.gameObject + "!"); return; }
         if (startFromTrigger && !triggerObject) { Debug.LogError("You have Start from trigger checked, but you forgot to apply the TriggerGO1 prefab!" + this.gameObject); return; }
         //-------------------------------------------------------
 
         if (points.Length > 2) { enableMultiPoints = true; }
         destination = points[currentPoint];
         if (startAutomaticly)
         {
             StartCoroutine(WaitTime());
         }
         else if (startFromTrigger)
         {
             go= false;
             GameObject trigger = Instantiate(triggerObject, transform.position + offset, transform.rotation) as GameObject;
         }
     }
 
     void Update()
     {
         destination = points[currentPoint];
         
         //So currentPoints isn't greater than points length. Or less than 0.
         if (currentPoint >= points.Length) { currentPoint = points.Length; }
         if(currentPoint <= 0) {currentPoint = 0;}
 
         if (go && enableMultiPoints)
         {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, destination.position, step);
 
 
             if (currentPoint == points.Length) { switchIt = true; }
             if (transform.position == destination.position && !switchIt)
             {
                 currentPoint++;
             }
             else if (transform.position == destination.position && switchIt)
             {
                 currentPoint--;
             }
 
             
         }
         else if (go && !enableMultiPoints)
         {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, destination.position, step);
 
             if (transform.position == points[1].position)
             {
                 destination = points[0];
             }
             else if (transform.position == points[0].position)
             {
                 destination = points[1];
             }
         }
 
     }
     private IEnumerator WaitTime()
     {
         //Wait this long.
         yield return new WaitForSeconds(delayStartBy);
         go = true;
     }
 
 }
 
Comment
Add comment · Show 1
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 JoshMBeyer · Sep 03, 2014 at 02:07 AM 0
Share

It's saying Array is out of bounds when it reaches the last destination. Since there are 4 points when it gets to points[3] it says array is out of bounds ins$$anonymous$$d of "switchIt" being true, and then going backwards through the array.

1 Reply

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

Answer by kacyesp · Sep 03, 2014 at 02:17 AM

At this line:

 if (currentPoint == points.Length) { switchIt = true; }

switchIt does become true, which is fine, but in the following else if statement

  else if (transform.position == destination.position && switchIt)

in the case that transform.position does not equal destination.position, currentPoint will not be decremented which is why you're going out of bounds.

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 JoshMBeyer · Sep 03, 2014 at 02:24 AM 1
Share

Awesome, I am very tired, been up way to long. lol Now to figure out how to fix this issue

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Find the average of 10 Vectors 4 Answers

Array "size" question. 1 Answer

JavaScript String Question 2 Answers

Instantiating random prefabs 1 Answer

How to make an array list set to false gradually instead of instantly? 2 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