moving platform script

public class WindMov : MonoBehaviour
{
    public GameObject wind;
    public float mSpeed = 5f;
 
    public Transform currentPosition;
    public Transform[] points;
    public int pointSelection = 0;

    public bool forward = true;
 
    // Start is called before the first frame update
    void Start()
    {    
        currentPosition = points[pointSelection];
    }

    private void Update()
    {   

        if (wind.transform.position == currentPosition.position)
        {
            if (forward == true)
            {

                pointSelection++;

            }
            else
            {
                pointSelection--;
            }
        

            if(pointSelection == points.Length && forward == true)
            {
                forward = false;
            }else if (currentPosition.position == points[0].position && forward == false)
            {
                forward = true;
            }

            currentPosition = points[pointSelection];
        }
    }

    void FixedUpdate()
    {

        wind.transform.position = Vector3.MoveTowards(wind.transform.position, currentPosition.position, Time.deltaTime * mSpeed);
    }
}

hi, I’m working on this script, that seems that is working perfectly, but, it displays me an error in the console, the script its suppose to make the gameObject to go along a route and after reaching the end, goes backwards just to start again, and it totally works, but it bugs me that it displays this error, can someone enlighten me about how to fix it? it seems to display whenever it changes between true and false on the boolean variable, each time it reachs one end of the array or the other

IndexOutOfRangeException: Index was outside the bounds of the array.
WindMov.Update () (at Assets/Scripts/Elements/WindMov.cs:47)

Please don’t duplicate post. As I replied in the Scripting forum,

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

Steps to success:

  • find which collection it is and what line of code accesses it <— critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection size
  • remember you might have more than one instance of this script in your scene/prefab
  • remember the collection may be used in more than one location in the code
1 Like

Yea, sorry about that, i didnt know hot to erase the previous post, and i thought this sub post would e better place to post it, anyway thanks pal

1 Like