Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 alexander11 · Sep 10, 2016 at 12:20 AM · c#unity 53dlinescreating

How do i create lines in scene view??

Hello i am having some trouble with Lists in unity what i am trying to do is create multiple lines in the scene view but i am getting some errors, does anyone know what i am doing wrong?

Here is my code so you know what i have done.

 using UnityEngine;
 using System.Collections.Generic;

 [ExecuteInEditMode]
 public class ClickLines : MonoBehaviour {
 
     bool c;
     List<Vector3> s = new List<Vector3>();
     List<Vector3> e = new List<Vector3>();
 
     void Update()
     {
         GetInput();
     }
     void GetInput()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Start();
         }
         else if (Input.GetMouseButtonUp(0))
         {
             End();
         }
     }
 
     void Start()
     {
         c = true;
         s = GetPoint();
         s.Add(GetPoint);
 
     }
     void End()
     {
         c = false;
         e = GetPoint();
     }
     Vector3 GetPoint()
     {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
             {
                 return hit.point;
             }
             return Vector3.zero;
     }
     void OnDrawGizmos()
     {
         Gizmos.DrawLine(s, e);
         Gizmos.DrawSphere(s, 0.4f);
         Gizmos.color = Color.blue;
         Gizmos.DrawSphere(e, 0.4f);
     }
 }
 
Comment
Add comment · Show 7
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 Addyarb · Sep 10, 2016 at 12:45 AM 0
Share

Looks like to me you're setting up lists of Vector3s, but you are setting that list to a Vector3 - which is not allowed.

Can you explain what you're trying to do?

avatar image AlucardJay · Sep 10, 2016 at 12:53 AM 0
Share

Posting error messages help identify the problem. I assume it's regarding this :

 Gizmos.DrawLine(s, e);

You need to specify the index:

 Gizmos.DrawLine(s[0], e[0]);

etc

avatar image alexander11 AlucardJay · Sep 10, 2016 at 01:14 AM 0
Share

The errors i get are:

  • CS1503 (5)

  • CS0029 (2)

I know i have to specify the index(i forgot to edit the OnDrawGizmos). but how do i add stuff to the list so i can make multiple lines

avatar image AlucardJay AlucardJay · Sep 10, 2016 at 01:53 AM 0
Share
 myList.Add(theItem); // add an item to the end of the List

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?#Generic_List

avatar image alexander11 AlucardJay · Sep 10, 2016 at 02:17 AM 0
Share

The wiki did not really help me. I still get the errors with this(what did i do wrong?).

     void Start()
     {
         c = true;
         for (int i = 0; i < s.Count; i++)
         {
             s[i] = GetPoint();
 
             s.Add(GetPoint);
         }
 
     }
     void End()
     {
         c = false;
         for (int j = 0; j < e.Count; j++)
         {
             e[j] = GetPoint();
         }
     }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by b1gry4n · Sep 10, 2016 at 04:51 AM

See the modified script. Note that creating a class containing the start and end of a line is an easier way to reference a "line". This will not work in the editor, you must press play. I am not familiar with creating editor scripts, hopefully this gives you a good idea how to achieve what youre after. Youll have to reference the editor camera rather than the main camera. There are questions already asked that cover that

alt text

 using UnityEngine;
 using System.Collections.Generic;
 
 public class Line
 {
     public Vector3 start;
     public Vector3 end;
     public Line(Vector3 s, Vector3 e)
     {
         start = s;
         end = e;
     }
 }
 
 
 //[ExecuteInEditMode]
 public class ClickLines : MonoBehaviour
 {
 
     bool c;
     private Vector3 s;
     private Vector3 e;
     public List<Line> lines = new List<Line>();
     //List<Vector3> s = new List<Vector3>();
     //List<Vector3> e = new List<Vector3>();
 
     void Update()
     {
         GetInput();
     }
     void GetInput()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Start();
         }
         else if (Input.GetMouseButtonUp(0))
         {
             End();
         }
     }
 
     void Start()
     {
         c = true;
         s = GetPoint();
 
 
     }
     void End()
     {
         c = false;
         e = GetPoint();
 
         lines.Add(CreateLine());
     }
 
 
     Line CreateLine()
     {
         Line l = new Line(s, e);
         return l;
     }
 
 
 
     Vector3 GetPoint()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit))
         {
             return hit.point;
         }
         return Vector3.zero;
     }
     void OnDrawGizmos()
     {
         if (lines.Count > 0)
         {
             foreach (Line l in lines)
             {
                 Gizmos.color = Color.red;
                 Gizmos.DrawLine(l.start, l.end);
                 Gizmos.color = Color.blue;
                 Gizmos.DrawSphere(l.start, 0.4f);
                 Gizmos.DrawSphere(l.end, 0.4f);
             }
         }      
     }
 }



z.jpg (53.1 kB)
Comment
Add comment · Show 2 · Share
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 AlucardJay · Sep 10, 2016 at 05:09 AM 0
Share

Start is a $$anonymous$$onoBehaviour function, this should really be renamed to something like Begin to avoid confusion and misfiring the function. (I realize this is what the OP named it)

avatar image alexander11 · Sep 10, 2016 at 06:23 AM 0
Share

Thanks :D.

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

239 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is Matrix4x4? 2 Answers

Hello i am having some trouble converting a JS to a C# script. 1 Answer

How do i Extrude a 2D mesh(or Model) from one point to another? 1 Answer

How can I generate a up facing quad mesh with adjustable res? 0 Answers

Is there a similar Command to lineRenderer.SetVertexCount for mesh?? 1 Answer


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