How To Draw Line On Screen In Unity (C#)

Draw Line On Screen - Unity (C#)

This is an interesting post on How To Draw Lines on the Screen in Unity (C#) using the LineRenderer component. The Unity documentation here manages to describe it in a very clear way. However, there is no info on how to add this via script i.e., during the runtime. We thought it would be nice to come up with an article which described this.

Create a new C# script and attach it to the Main Camera. Open the script and paste the following lines of code:

using UnityEngine;
using System.Collections;

public class DrawLine : MonoBehaviour
 //reference to LineRenderer component
 private LineRenderer line; 
 //car to store mouse position on the screen
 private Vector3 mousePos;
 //assign a material to the Line Renderer in the Inspector
 public Material material;
 //number of lines drawn
 private int currLines = 0;

 void Update ()
  //Create new Line on left mouse click(down)
   //check if there is no line renderer created
   if(line == null){
    //create the line
   //get the mouse position
   mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   //set the z co ordinate to 0 as we are only interested in the xy axes
   mousePos.z = 0;
   //set the start point and end point of the line renderer
  //if line renderer exists and left mouse button is click exited (up)
  else if(Input.GetMouseButtonUp(0) && line)
   mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   mousePos.z = 0;
   //set the end point of the line renderer to current mouse position
   //set line as null once the line is created
   line = null;
  //if mouse button is held clicked and line exists
  else if(Input.GetMouseButton(0) && line)
   mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   mousePos.z = 0;
   //set the end position as current position but dont set line as null as the mouse click is not exited
   line.SetPosition(1, mousePos);
 //method to create line
 private void createLine()
  //create a new empty gameobject and line renderer component
  line = new GameObject("Line"+currLines).AddComponent<LineRenderer>();
  //assign the material to the line
  line.material =  material;
  //set the number of points to the line
  //set the width
  //render line to the world origin and not to the object's position
  line.useWorldSpace = true;  


Save the script and return to Unity. You should notice that the script expects a material to be attached. Create a material if you don't have one already and add it to the script.
Play the scene and you should notice that there is nothing on the scene. Left click your mouse on the screen and move it while the button is clicked down, you should notice that the line is being drawn on the screen.

See you around.
Share on Google+

About Unknown

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.


  1. Excellent Post!!
    But i want to know how to restrict play to draw a single line.
    Thanks in advance!!

  2. Hi, thank you for the script - this works well!
    Can I please have some advice on how to convert this script to draw non-straight lines like squiggly lines (again 2D) with many vertices?
    Thank youuu


  3. thx you, sooooo helpful !
    but if I try to give sprite like background, the line can't show, it's always hide under the bckground..
    how can I make the line keep visible even if I add some object?

    thk you very much for your answer

  4. We expect that the new LG V30 will be a super success when it launches at the upcoming global event. LG V30 Release Date

  5. It is not working for me in unity 5.5 editor

    1. Finally it works for me as well. LOL .

      only change was:

      mousePos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,
      Input.mousePosition.y, Camera.main.nearClipPlane));

  6. hello, can you tell me how can i destroy the line after a few seconds? an also i woud like to add to ciecles at the start and the end of the line thanks.

  7. Discover spying opportunities with new webwatcher application.

  8. Amazing! Visit also my similar website here!

  9. This comment has been removed by the author.

  10. Excellent work ! i ask how i can add Rigidbody to line !
    how i can get a line physical ?

  11. Thanks for sharing these useful information! hotmail login free

  12. Nice article and sharing of information is worth reading too


  13. Excellent blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative.
    oracle fusion financials classroom training
    Workday HCM Online Training
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training
    Oracle Fusion HCM Classroom Training