Accessing LineRenderer in C#

I’ve tried changing one of the vertexes on a LineRenderer in C#, but I’m getting an error:

gameObject.renderer.SetPosition(0, new Vector3(0f, 0f, 0f);

But I’m getting an error that says that SetPosition() is not defined for GameObject.Renderer.

Do I need to cast renderer somehow? The LineRenderer was added in the scene.

Renderer is the super class of LineRenderer, so the renderer component knows nothing of SetPosition().

What you most likely want to do is to simply use GetComponent().

(gameObject.GetComponent(LineRenderer)).SetPosition(0, new Vector3(0f, 0f, 0f);

What can also be convenient in addition to saving a little bit of performance is to make a private var in the script called something like lineRenderer and then just GetComponent() once in the Start function.

Cheers,
-Jon

I thought that might be the case, obviously GetComponent() is my friend…

Hi,
When Iam trying to Assign my linerenderer object to the gameobject (not in start()) its throughing error.

This post is over 11 years old. Wow. Please create a thread with your issue and relevant code for some assistance. :slight_smile: