Creating a LineRenderer without adding it as a component?

Hey,

So I had some trouble with generating shapes, so I wrote up a script for it.
I want to make it universal so anyone can use it, but I’ve hit a snag-

I think I’d need to do the following:

LineRenderer line = new LineRenderer();

Though, this doesn’t work, of course. The normal way to do it is add it as a component before modifying its values. Except I cant do that in this instance. Heres my code.

using UnityEngine;
using System.Collections.Generic;

public static class UsefulMethods {

		//Gives the option to not add a material.
	public static LineRenderer DrawShape(int numberOfVerticies, float radius, Vector3 position){
		return ShapeConstructor(numberOfVerticies, radius, position, radius/10, radius/10, null);
	}
		//Gives the option to add a material.
	public static LineRenderer DrawShape(int numberOfVerticies, float radius, Vector3 position, Material indicatorMaterial){
		return ShapeConstructor(numberOfVerticies, radius, position, radius/10, radius/10, indicatorMaterial);
	}
		//Gives the option to specify the thickness of the line at the start and end
	public static LineRenderer DrawShape(int numberOfVerticies, float radius, Vector3 position, float lineStartThickness, float lineEndThickness){
		return ShapeConstructor(numberOfVerticies, radius, position, lineStartThickness, lineEndThickness, null);
	}
		//Gives the option to specify the thickness of the line at the start and end, and add a material.
	public static LineRenderer DrawShape(int numberOfVerticies, float radius, Vector3 position, float lineStartThickness, float lineEndThickness, Material indicatorMaterial){
		return ShapeConstructor(numberOfVerticies, radius, position, lineStartThickness, lineEndThickness, indicatorMaterial);
	}
		//This constructor shouldn't be called, but would work fine if you make it public and call it.
	static LineRenderer ShapeConstructor(int numberOfVerticies, float radius, Vector3 position, float lineStartThickness, float lineEndThickness, Material indicatorMaterial){
			//Make a list for the points
		List<Vector3> points = new List<Vector3>();
			//Get the angles in Degrees between the points
		float degBetweenPoints = 360 /numberOfVerticies;
			//A for loop that loops for each point
			//Note the '+1' This is to ensure the shape closes,
			//otherwise it doesn't finish.
		for(int i = 0; i < (numberOfVerticies + 1); i++){
				//Get the X and Y of each point and add it to the list.
				//the forumae I used are:
				//
				// x = r * Cos(Theta)
				// y = r * Sin(Theta)
				//
				//Where Theta is the change in radians between each point
				//If you don't convert degrees to radians (The Pi/180 part)
				//You end up with a horrible criss-cross shape.
			float x = (radius) * Mathf.Cos ((degBetweenPoints * (Mathf.PI/180)) * i);
			float y = (radius) * Mathf.Sin ((degBetweenPoints * (Mathf.PI/180)) * i);
			points.Add (new Vector3(x,y,0));
		}
			//Create the line renderer to draw the points.
		LineRenderer line = new LineRenderer();
			//Set the line thickness
		line.SetWidth (lineStartThickness,lineEndThickness);
			//Set the amount of verticies there are
			//This stops an array out of bounds error
		line.SetVertexCount(points.Count);
			//Sets the material
		line.material = indicatorMaterial;
			//Adds the points, one by one, to the line renderer
		for(int i = 0; i < points.Count; i++){
			line.SetPosition (i,points *+ position);*
  •  }*
    
  •  return line;*
    
  • }*
    }

It works when adding the component, but the static nature of it means I can’t do that, as far as I’m aware. Does anyone know how to get around this? Thanks.

Replace

LineRenderer line = new LineRenderer();

with:

LineRenderer line = (new GameObject("line")).AddComponent<LineRenderer>();

there’s no way around attaching the LineRenderer to a gameobject. It’s a component. Components can’t live on their own. And even if they could they won’t “do” anything as everything is bound to GameObjects in Unity.