- Home /
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);
}
}
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?
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
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
myList.Add(theItem); // add an item to the end of the List
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();
}
}
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
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);
}
}
}
}
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)
Your answer
Follow this Question
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