0
\$\begingroup\$

I am trying to get the coordinates or positions that are generated at the time I draw with linerenderer, but it gives me an error.

This may be a rookie mistake and I am not using the Linerenderer's methods well. I would be very grateful for your help.


LineRenderer.GetPosition index out of bounds! UnityEngine.LineRenderer:GetPosition(Int32) sendData_Figura:Trazo(Int32) (at Assets/sendData_Figura.cs:115) sendData_Figura:Update() (at Assets/sendData_Figura.cs:93)


public LineRenderer  line;
public static int contadorTrazo = 0;

void Update(){    
    Trazo();
}


void Trazo() {
    line = line.GetComponent<LineRenderer>();
    Debug.Log("Trazo : "+line.GetPosition(contadorTrazo));
    contadorTrazo++;
}
\$\endgroup\$
2
  • \$\begingroup\$ "it gives me an error" - great! Error messages are extremely useful in tracking down the source of the problem. What specific error does it give? We can't see anything you don't show us - including where you define your line variable. \$\endgroup\$ – DMGregory Sep 3 '19 at 3:18
  • \$\begingroup\$ @DMGregory ese es el error que me esta dando \$\endgroup\$ – Nicolas Flores Muñoz Sep 3 '19 at 3:25
0
\$\begingroup\$

First, this line does nothing of use:

line = line.GetComponent<LineRenderer>();

line already points to your LineRenderer. You don't need to say "assign line to continue being itself" ;)

Second: your problem is just that you're continuing past the end of the line.

LineRenderer.GetPosition index out of bounds!

This means yo tried to access, say, point number 5 in a line that contains only 4 points. There's no sensible answer Unity can give you here, so it throws an error to tell you to correct your question.

You need to either stop incrementing contadorTrazo when you reach the last point in the line, or wrap back around to the beginning / bounce back / whatever behaviour is appropriate for your application:

void Trazo() {

    if(contadorTrazo < line.positionCount) {
        Debug.Log("Trazo : "+line.GetPosition(contadorTrazo));
        contadorTrazo++;
    } else {
        // What do you want to do at the end of the line?
        // Maybe restart at zero?
        contatorTrazo = 0;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you very much, what I am trying to do get the vertices or coordinates and recorder in a database. Seriously thanks for the help. \$\endgroup\$ – Nicolas Flores Muñoz Sep 3 '19 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.