Line Renderer

Hey,
I have an idea for my game but couldnt figure out how im supposed to make it work. When the Player Collide with an object a line should appeare between the Player and the object and when the player moves the line should move too. I tried withe 3 scripts but then the console say:
NullReferenceException: Object reference not set to an instance of an object
LrTesting.Update () (at Assets/LrTesting.cs:13)
```csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineController : MonoBehaviour
{
public LineRenderer lr;
public Transform points;

private void Awake()
{
    //if (lt.LineConect == 2)
    //{
    lr = GetComponent<LineRenderer>();
    //}
}

public void SetUpLine(Transform[] points)
{
    //if(lt.LineConect == 2){
    lr.positionCount = points.Length;
    this.points = points;
    //}
}
private void Update()
{
    //if (lt.LineConect == 2)
    //{
    for (int i = 0; i < points.Length; i++)
    {
        lr.SetPosition(i, points[i].position);
    }
    //}
}

}**
** **csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineTower : MonoBehaviour
{
public int LineConect = 1;
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == “Player”)
{
LineConect = 2;
Debug.Log(“Do it”);
}
}
}**
** **csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LrTesting : MonoBehaviour
{
[SerializeField] private Transform points;
[SerializeField] private LineController line;
public LineTower lt;

private void Update()
{
    if (lt.LineConect == 2)
    {
        line.SetUpLine(points);
    }
    else
    {
        Debug.Log("Please");
    }
}

}**
```

A null reference exception essentially means you tried to use a variable that didn’t have a real value assigned to it (usually by writing a period after the variable, like in “myVariable.something”).

In what I assume to be LrTesting.cs line 13, the only variable you are dereferencing is “lt”. Looks like that’s a public variable. I’m guessing that you meant to assign a value to it through the inspector but forgot to do so.

1 Like

Thank you for explaining the problem and what it means. Thas really cool!