- Home /
LineRenderer SetPositions gives a SetPosition index out of bounds
I believe that LineRenderer.SetPositions is not behaving correctly. When given an array longer than what was set with SetVertexCount, it always shows an index out of bounds error, even though the points within the count are available. One is forced to pass an array which length matches the SetVertexCount number. This seems inefficient as one should be able to maintain a larger array that is ready to handle a short or long line. The line is drawn correctly either way.
The script below is the simplest way to show the problem. In both cases, the line is drawn properly, but one way throws an error.
using UnityEngine;
using System.Collections;
public class LR_Bug : MonoBehaviour {
/*
* Default Empty Scene
* Attach a LineRenderer to the Main Camera, and this script to the Main Camera
*
* */
private LineRenderer LR;
private Vector3[] points10, points2;
private int evenOrOddSeconds;
// Use this for initialization
void Start () {
LR = this.GetComponent<LineRenderer> ();
// create an array with 10 items, but only init 2
points10 = new Vector3[10];
points10 [0] = new Vector3 (0F, 0F, 10F);
points10 [1] = new Vector3 (10F, 10F, 10F);
// create an array with 2 items, and init both
points2 = new Vector3[2];
points2 [0] = new Vector3 (10F, 0F, 10F);
points2 [1] = new Vector3 (20F, 10F, 10F);
}
// Update is called once per frame
void Update () {
// swap lines every second
evenOrOddSeconds = (int)(Time.time % 2);
if (evenOrOddSeconds == 0) {
/*
you will get "LineRenderer.SetPosition index out of bounds!"
even though you are saying to use only the first two elements
which are properly initialized
The LR does draw the line correctly.
*/
Debug.Log ("Using an array of length 10, but only asking to draw 2 - shows error");
LR.SetVertexCount (2);
LR.SetPositions (points10);
} else {
/*
* array matches vertext count - no error
*
*/
Debug.Log ("Using an array of length 2, no error");
LR.SetVertexCount (2);
LR.SetPositions (points2);
}
}
}
Can anyone reproduce this? Should I not worry about the error? Should I not worry about having to create a new array of the proper size every time I need to update the line, instead of keeping track of a single larger array?
Thank you,
Andres
Your answer
Follow this Question
Related Questions
How to keep other Attributes when working with custom ones? 0 Answers
Level Select screen Messed Up - Please help! 0 Answers
Unity can't use the script. 0 Answers
Bug With My Level Select Screen 2 Answers