Deleting a Line Renderer

Hey Guys

I’m shooting a laser using a line renderer in code. So when I hit fire, I make two vertex points, set the beginning point and the end point, thats fine. But now that i’m trying to delete it I’m running into some problems. I have to set the vertices back to 0 when I want it to delete, so basically after I’ve fired in the update function is where I would put it would be my assumption. My code looks like this.

void Update () 
{
       if(Input.GetButtonDown("Gamepad " + mannedPlayer + " RB")  isRightTrigger == true)
	{
	  if(isReloading == false)
	  {
		renderLaser();
          }
        }
	else 
	{
		lineRenderer.SetVertexCount(0)
				
	}
}

Now the problem with the above code, is that it deletes too quickly. I can’t figure out how to go about adding a delay to that properly to slow the update down. I’ve tried yield waitforseconds as well as Invoke. If anyone can figure out a good way to do this, that would be awesome.

Thanks

How about calling Destroy?

Destroy(lineRenderer.gameObject, 5);

It will destroy the object in 5 seconds.

PS
However, if you don’t want to destroy it completely, I’d use something like that: A Replacement for Coroutines in Unity + C# – The Instruction Limit

Usage:

Wait.Until(elapsed => elapsed > 5, () => lineRenderer.enabled = false);
1 Like

See I’m afraid of if I destroy it, then I won’t be able to shoot again. It’s a turret so it’s constantly being fired.

Any help would be great ._.

Invoke didn’t work as expected?

	...
	Invoke("DisableLineRenderer", 5);
	...

private void DisableLineRenderer()
{
	lineRenderer.SetVertexCount(0);
}

Well 12 years later I had the same question!
What worked for me is this:

lineRenderer.SetPositions(
                    new Vector3[] { Vector3.zero, Vector3.zero });
lineRenderer.enabled = false;

(I had only two points!)

you can disable and enabling it each time, or use a pooling system with 10 line renders per example and enable and disable them as much as you want.