Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gl0ck · Oct 26, 2014 at 02:19 PM · nullreferenceexceptiondrawline renderer

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/

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image drudiverse · Oct 26, 2014 at 03:45 PM 0
Share

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.

avatar image gl0ck · Oct 27, 2014 at 06:23 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Line Renderer Ending In the Center 0 Answers

Null Reference Exception when using variable in another script 2 Answers

Scene Null Reference in build 1 Answer

Why does ScriptableObject.CreateInstance, where T is definite at runtime, return null? 1 Answer

2D Game Checkpoint & Respawning 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges