- Home /
Destroying a LineRenderer component and then adding it again at runtime
I have a global var lineRenderer:LineRenderer
that is:
- initiated in
Start()
vialineRenderer = gameObject.AddComponent(LineRenderer);
- has its
setPosition()
populated with a bunch of coordinates - so it appears like a "drawn line" on a whiteboard
And then I want to erase the board - presumably, the way to do that is Destroy()
... so I tried Destroy(lineRenderer);
When I attempt to reinitiate it with lineRenderer = gameObject.AddComponent(LineRenderer);
, I get errors like: "Can't add component 'Line Renderer' to lRenderer (name of game object) because such a component is already added to the game object!"
Subsequent lineRenderer.property assign's yield errors like: "Object reference not set to an instance of an object"
So, long description short, how do you ERASE the line from a lineRenderer, so that you can repopulate its setPosition? (Not sure if destroy is the right word..)
Answer by Loius · Dec 26, 2010 at 03:03 PM
By reading the documentation, it looks like you want
GetComponent(LineRenderer).SetVertexCount(0)
No. Error still occurs. I had that in there before, referenced as lineRenderer.SetVertexCount(0) -- to be sure, I tried GetComponent(LineRenderer).SetVertexCount(0);
- same error still occurs
Answer by Nomad the Grey · Mar 01, 2011 at 06:22 PM
Although I realize that I am replying to this post a couple of months later than necessary, perhaps I can help somebody anyway. This error exists in ordinary .NET apps. When I see this error, I fix the problem by inserting code similar to:
lineRender = new LineRenderer();
Hope this helps.
Answer by RyanZimmerman87 · Mar 09, 2013 at 02:22 AM
public LineRenderer lineRenderer;
Start()
{
lineRenderer = gameObject.GetComponent<LineRenderer>();
}
Update()
{
if (your desired conditions)
{
lineRenderer.SetVertexCount(0);
}
That seems to be working for me, hope this helps.
Answer by liamrasoull · Apr 28 at 06:49 PM
you can use lineRenderer.positionCount = 0;
lineRenderer.SetVertexCount(0); is outdated.
Your answer
Follow this Question
Related Questions
Clearing a LineRenderer Whiteboard, so that you can start drawing again 1 Answer
Export objects to a .3DS file at runtime 1 Answer
Destroy the first instanced prefab while still being able to generate more prefab's later 1 Answer
Is there such thing as a recommended Component cap? 0 Answers
AddComponent performance 1 Answer