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

Visual Studios Not Recognizing LineRenderer.positionCount

Discussion in 'Scripting' started by Phanixis, May 31, 2017.

  1. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    The title says it all. I am trying to make a multi-segmented line with more than two vertexes using the line render from within C sharp. Rendering a single line segment with only two vertexes is no problem. But if I try an add another vertex Unity will produce an index out of bounds error upon running. So I looked through the documentation and it appears I need to set LineRender.positionCount equal to however many vertices I plan to use, so if I have a pair of line segments with three vertices I would need to call

    LineRenderer.positionCount = 3

    The problem is, despite appearing in the Unity documentation, visual studios does not recognize positionCount as a method associated with the line renderer. Anyone know what is going on here?

    Thanks for the help.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's because you're trying to access it as though it were a property on the class, and not on an instance.
    You might try:
    Code (csharp):
    1. LineRenderer lr = GetComponent<LineRenderer>();
    2. lr.positionCount = 3;
    3.  
    You can also make the 'lr' variable public or give it a SerializeField attribute and drag & drop it in the inspector, etc..(also).

    Reference (with example): https://docs.unity3d.com/ScriptReference/LineRenderer-positionCount.html
     
  3. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    Sorry, I guess I wasn't being clear enough. I am attempting to access it in precisely the manner that you are describing, but it is not working. Here is the exact code:

    //Prepare line rendering variables
    private LineRenderer lr;

    void Start()
    {
    //Prepare the linerenderer
    //lr = new GameObject("Dragline", typeof(LineRenderer));
    lr = GetComponent<LineRenderer>(); //Attach a linerender component to the inherited object (the ship)

    //Set the material
    lr.material = new Material(Shader.Find("Sprites/Default"));
    //lr.material = new Material(Shader.Find("Unlit/Texture"));

    //Set the color
    lr.SetColors(Color.red, Color.red);

    //Set the dimensions
    lr.SetWidth(0.1f, 0.1f);

    lr.positionCount = currentPath.Count;

    //Set positions for testing
    lr.SetPosition(0, new Vector3(0, 0, -1f));
    lr.SetPosition(1, new Vector3(1f, 0, -1f));
    lr.SetPosition(2, new Vector3(1f, 1f, -1f));


    }

    And here is the error message I get

    Severity Code Description Project File Line
    Error CS1061 'LineRenderer' does not contain a definition for 'positionCount' and no extension method 'positionCount' accepting a first argument of type 'LineRenderer' could be found (are you missing a using directive or an assembly reference?) CEN.CSharp D:\Projects\Unity\CEN\Assets\_Scripts\Unit.cs 43

    Also note that all the other linerenderer methods such as SetWidth and SetColors work normally, it is only positionCount that is generating an error (and SetPosition, but only because the index on the third line is out of bounds).

    Hopefully that clears things up.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  5. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    I am using version 5.3.2. I guess they changed the name of the function recently.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yep. You got it working now?
     
  7. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    I just upgraded to the latest version. Looks like the linerenderer was the only thing that had obsolete code in any case. Also had to reset some materials for some reason. It all seems to work now. Thanks for the help.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool. You're welcome, np. :)