Hard time update start and end coordinates of a line

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?

Try:
LineRenderer lr = myLine.AddComponent(); //instead of Lines 11 and 12
AddComponent should return the added Component
I am not sure if Components need the end of an Update run to settle in and therefor your code fails.

EDIT: Look at FernandoHCs post. He’s totally right.

That’s a simple definition out of scope issue.
You’re not defining/assigning a value to lr before using it in the else condition.

1 Like

Do NOT do the above: if you ever make a texture called “Material.png” (for example), your code will begin failing half the time randomly.

Always use Resources.Load<T>(), never use Resources() as T

https://discussions.unity.com/t/847547/4

I improved my code with your recommendations, it works even if i think i can improve it.

if (lineEastExist == false)//id don't exist create it
                {
                    GameObject myLine = new GameObject();
                    myLine.gameObject.name = "eastLine";
                    myLine.gameObject.tag = "eastLine";

                    myLine.transform.position = startLine;
                    LineRenderer lr = myLine.AddComponent<LineRenderer>();

                    Material mat;
                    mat = Resources.Load<Material>("Material");
                    lr.material = mat;
                    lr.startColor = lineColor;
                    lr.endColor = lineColor;

                    lr.startWidth = 2f;
                    lr.endWidth = 2f;

                    lr.SetPosition(0, startLine);
                    lr.SetPosition(1, endLine);

                    lineEastExist = true;
                }
                else//if exist update it
                {
                    name_ = GameObject.FindWithTag("eastLine");
                    LineRenderer lr = name_.GetComponent<LineRenderer>();
                    lr.SetPosition(0, startLine);
                    lr.SetPosition(1, endLine);
                }
            }