Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Pauls · Mar 22, 2013 at 05:01 AM · linerendererremove

Remove points of LineRenderer?

Hi,

I found this thread about how to add line with "LineRenderer" after a click (http://answers.unity3d.com/questions/377596/how-do-you-draw-a-line-using-your-finger-on-androi.html), 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[i]);    
             }
         }
         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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Mar 22, 2013 at 05:44 AM

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[i]);    
         }
     }
     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().

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pauls · Mar 22, 2013 at 06:10 AM 0
Share

You are right, thank you very much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

LineRenderer GetPosition? not there. 2 Answers

Removing the first point form a lineRenderer 1 Answer

Remove color of an object 2 Answers

When removing something from a list it says missing 1 Answer

Raycast trail renderer? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges