Need Help To Instantiate A Line

Hey guys, I’m working on drawing a straight line with the mouse, it’s working to a degree with a cylinder, but I need to be able to create multiple’s of that object, not just have the one that disappears every time a new line is drawn.

So obviously the best solution is to Instantiate a Prefab of this cylinder, but I’m not sure how to go about it, I worked with instantiating things before, but this one is different and I’ve looked for similar problems but I haven’t been able to solve this myself.

And help is greatly appreciated!

using UnityEngine;
using System.Collections;

public class Lines: MonoBehaviour 
{
	Vector3 pos1; 
	Vector3 pos2; 
	float objectHeight= 2.0f;
	
	void  Update ()
	{

		Time.timeScale = 0.2f;
		if (Input.GetMouseButtonDown(0)) 
		{
			pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
			pos1 = Camera.main.ScreenToWorldPoint(pos1); 
			pos2 = pos1;
		}
		
		if (Input.GetMouseButton(0)) 
		{
			pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
			pos2 = Camera.main.ScreenToWorldPoint(pos2); 
		}
		
		if (pos2 != pos1) 
		{
			Vector3 v3= pos2 - pos1;
			transform.position = pos1 + (v3) / 2.0f;
			transform.localScale = new Vector3(transform.localScale.x, v3.magnitude/objectHeight, transform.localScale.z);
			transform.rotation = Quaternion.FromToRotation(Vector2.up, v3);
		}
	}
}

This script is attached to a cylinder in the scene scaled at 0.08-1-0.08. Thanks in advanced!

Also, I’m trying to do this in a 2D environment, does anyone know how I can do this without the z direction being used? In otherwords, I don’t want anything to be able to move in the z direction, just x and y across the screen.

I believe the code above is based on code I posted awhile back. Here is a changed version that uses a prefab. I tested it with a cylinder prefab with perspective camera and a scale of (0.01, 0.0, 0.01) and a orthographic camera with a prefab cylinder having scale (0.1, 0.0, 0.1).

using UnityEngine;
using System.Collections;

public class Lines: MonoBehaviour 
{
	public GameObject prefab;

	Vector3 pos1; 
	Vector3 pos2; 
	float objectHeight= 2.0f;
	Transform current = null;
	
	void  Update ()
	{
		Time.timeScale = 0.2f;
		if (Input.GetMouseButtonDown(0)) 
		{
			pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
			pos1 = Camera.main.ScreenToWorldPoint(pos1); 
			pos2 = pos1;
			current = (Instantiate(prefab, pos1, Quaternion.identity) as GameObject).transform;
		}
		
		if (Input.GetMouseButton(0)) 
		{
			pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
			pos2 = Camera.main.ScreenToWorldPoint(pos2); 
		}
		
		if (current != null && pos2 != pos1) 
		{
			Vector3 v3 = pos2 - pos1;
			current.position = pos1 + (v3) / 2.0f;
			current.localScale = new Vector3(current.localScale.x, v3.magnitude/objectHeight, current.localScale.z);
			current.rotation = Quaternion.FromToRotation(Vector2.up, v3);
		}

		if (Input.GetMouseButtonUp (0)) {
			current = null;
		}
	}
}

Note I do not understand this line in your question:

how I can do this without the z
direction being used?