Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Accessing LineRenderer in C#

Discussion in 'Scripting' started by CoherentInk, Dec 6, 2006.

  1. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    I've tried changing one of the vertexes on a LineRenderer in C#, but I'm getting an error:

    Code (csharp):
    1. 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.
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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().

    Code (csharp):
    1. (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
     
  3. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    I thought that might be the case, obviously GetComponent() is my friend...
     
  4. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    Hi,
    When Iam trying to Assign my linerenderer object to the gameobject (not in start()) its throughing error.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This post is over 11 years old. Wow. Please create a thread with your issue and relevant code for some assistance. :)