Hello everyone,
I have some difficulties to update the end / start coordinates of a line i draw.
This is how i draw a line, it works well :
void DrawLine(Vector3 start, Vector3 end, Color color)
{
GameObject myLine = new GameObject();
myLine.name = "Line";
myLine.transform.position = start;
myLine.AddComponent<LineRenderer>();
LineRenderer lr = myLine.GetComponent<LineRenderer>();
Material mat;
mat = Resources.Load ("Material") as Material;
myLine.GetComponent<LineRenderer>().material = mat;
myLine.GetComponent<LineRenderer>().startColor = color;
myLine.GetComponent<LineRenderer>().endColor = color;
myLine.GetComponent<LineRenderer>().startWidth = 2f;
myLine.GetComponent<LineRenderer>().endWidth = 2f;
lr.SetPosition(0, start);
lr.SetPosition(1, end);
}
I tried to adapt this function in order to update a line coordinates.
With the exemple below i have “UnassignedReferenceException: The variable lr of Laser has not been assigned.”
//here i update startLine end endLine
startLine = (transform.position);
endLine = new Vector3(transform.position.x + 2000f, transform.position.y, transform.position.z);
if (lineEastExist == false)//id don't exist create it
{
GameObject myLine = new GameObject();
myLine.transform.position = startLine;
myLine.AddComponent<LineRenderer>();
LineRenderer lr = myLine.GetComponent<LineRenderer>();
Material mat;
mat = Resources.Load("Material") as Material; // or mat = Resources.Load<Material>("Material");//requiere a material in Assets/Resources
lr.material = mat;
lr.startColor = lineColor;
lr.endColor = lineColor;
lr.startWidth = 2f;
lr.endWidth = 2f;
lineEastExist = true;
}
else//if exist update it
{
lr.SetPosition(0, startLine);
lr.SetPosition(1, endLine);
}
Can you help me?