- Home /
Draw line errors
Hello,
I have a question about drawing lines dynamically. The thing is that I found a tutorial with a sample code, but I cannot make it, asked there but the last answered question is 4 months ago so I am looking here for help. So, this is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawLine : MonoBehaviour
{
private LineRenderer line;
private bool isMousePressed;
private List<Vector3> pointsList;
private Vector3 mousePos;
// Structure for line points
struct myLine
{
public Vector3 StartPoint;
public Vector3 EndPoint;
};
// -----------------------------------
void Start()
{
// Create line renderer component and set its property
line = gameObject.AddComponent<LineRenderer>();
line.material = new Material(Shader.Find("Particles/Additive"));
line.SetVertexCount(0);
line.SetWidth(0.1f,0.1f);
line.SetColors(Color.green, Color.green);
line.useWorldSpace = true;
isMousePressed = false;
pointsList = new List<Vector3>();
// renderer.material.SetTextureOffset(
}
// -----------------------------------
void Update ()
{
// If mouse button down, remove old line and set its color to green
if(Input.GetMouseButtonDown(0))
{
isMousePressed = true;
line.SetVertexCount(0);
pointsList.RemoveRange(0,pointsList.Count);
line.SetColors(Color.green, Color.green);
}
else if(Input.GetMouseButtonUp(0))
{
isMousePressed = false;
}
// Drawing line when mouse is moving(presses)
if(isMousePressed)
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z=0;
if (!pointsList.Contains (mousePos))
{
pointsList.Add (mousePos);
line.SetVertexCount (pointsList.Count);
line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
}
}
}
}
Can someone tell me what I am doing wrong? I create an empty object, add the script to the object and place a plane down with black texture.
This is the error I get and two warnings: NullReferenceException: Object reference not set to an instance of an object DrawLine.Awake () (at Assets/Scripts/DrawLine.cs:20)
Assets/Scripts/DrawLine.cs(6,30): warning CS0649: Field DrawLine.line' is never assigned to, and will always have its default value
null' Assets/Scripts/DrawLine.cs(7,22): warning CS0414: The private field `DrawLine.isMousePressed' is assigned but its value is never used
This is the website where I found the code: http://www.theappguruz.com/tutorial/draw-line-mouse-move-detect-line-collision-unity2d-unity3d/
i just tried the linerenderer that is in the fractal tree code on the forums. i dont understand the dependencies of that code.
it sounds like you want to assign a line renderer to the object ins$$anonymous$$d of putting a plane on it.
check the GO has a line renderer when it runs.
Do you mean GL drawing lines? I looked at it and it is doing straight lines, but I need more complex curves, basically a brush on mouse left button pressed down and mouse dragged around.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170005im_/https://answers.unity.com/themes/thub/images/avi.jpg)