Remove points of LineRenderer?

Hi,

I found this thread about how to add line with “LineRenderer” after a click (How do you Draw a Line Using your Finger's Position on Android - Questions & Answers - Unity Discussions), and I would like to remove the points already drawn after 1 second, but my “removePoint” function does not work:

It seems to remove a slight line in the middle of the lines created, but the previous lines stay as is on screen (for example, if I click anywhere and move the mouse, all the lines previously created will remain, even after 1 second).

Would you know why this does not work? I have tried to remove the first point at index 0 with “RemovePoint” at the same rate as “AddPoint” : this code is in C#, and “AddPoint” works fine with just a “LineRenderer” and the script attached to the camera :

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TestLine : MonoBehaviour {

	LineRenderer lineRenderer;
	List<Vector3> myPoints;
	private float timeCount;
	private int indexRemove;

	// Use this for initialization
	void Start () {
		myPoints = new List<Vector3>();
		lineRenderer = GetComponent<LineRenderer>();
		timeCount = 0;
    	lineRenderer.SetWidth(0.2f,0.2f);
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(myPoints != null){//myPoints != null
	        lineRenderer.SetVertexCount(myPoints.Count);
	        for(int i = 0;i<myPoints.Count;i++){
	            lineRenderer.SetPosition(i,myPoints*);* 
  •      }*
    
  •  }*
    
  •  else*
    
  •  lineRenderer.SetVertexCount(0);*
    
  •  if (timeCount>1){*
    
  •  	InvokeRepeating("RemovePoint",.02f,.02f);*
    
  •  }*
    
  •  if(Input.GetMouseButtonDown(0)){*
    
  •      InvokeRepeating("AddPoint",.02f,.02f);*
    
  •  }* 
    
  •   if(Input.GetMouseButtonUp(0)){*
    
  •      CancelInvoke();*
    
  •      myPoints = null;*
    
  •  }*
    
  • }//end Update*

  • private void RemovePoint(){*

  •  myPoints.RemoveAt(0);*
    
  • }*

  • private void AddPoint(){*

  •  int j = 0;*
    
  •  List<Vector3> tempPoints = new List<Vector3>();*
    
  •  if(myPoints != null){*
    
  •  	for(j = 0; j < myPoints.Count; j++)* 
    
  •  		tempPoints.Add(myPoints[j]);*
    
  •  }*
    
  •  Vector3 tempPos = new Vector3();*
    
  •  tempPos = Input.mousePosition;*
    
  •  tempPos.z = 5;*
    
  • tempPoints.Add( Camera.main.ScreenToWorldPoint(tempPos) );*
    
  • myPoints = new List<Vector3>();* 
    
  • myPoints = tempPoints;* 
    
  • }*

}
Thanks

The reason nothing is removed after 1 second is that timeCount is never incremented. That is, typically you would see a line like…

lineCount += Time.deltaTime; 

…in the update loop. But making that change does not fix this code because it will result in InvokeRepeating() getting called each frame. I’ve modified Update() to give you what I think you want:

void Update () {
 
   if(myPoints != null){//myPoints != null
        lineRenderer.SetVertexCount(myPoints.Count);
        for(int i = 0;i<myPoints.Count;i++){
            lineRenderer.SetPosition(i,myPoints*);* 

}
}
else
lineRenderer.SetVertexCount(0);

if(Input.GetMouseButtonDown(0)){
InvokeRepeating(“AddPoint”,.02f,.02f);

  •  InvokeRepeating ("RemovePoint", 1.0f, 0.02f);*
    

}
if(Input.GetMouseButtonUp(0)){
CancelInvoke();
myPoints = null;
}
}//end Update
Paste that in place of your existing Update().