Home > Net >  Unity Public-Private In Line Renderer
Unity Public-Private In Line Renderer

Time:08-10

There is a a question that I did not solve yet.

In LineRenderer when I write public list of point's positions line in game is seen however when I write private list of point's positions line in game is not seen. Why? Is there another thing like this in unity which I should know? Thanks.

If I change from private List wayPointsPositions; to public List wayPointsPositions; line is perfectly working.

 private LineRenderer lineRenderer;
private GameObject[] wayPointsObject;
private List<Vector3> wayPointsPositions;

void Start()
{
    lineRenderer = GetComponent<LineRenderer>();
    wayPointsObject = GameObject.FindGameObjectsWithTag("WayPoints");
    for (int i = 0; i < wayPointsObject.Length; i  )
    {
        wayPointsPositions.Add(wayPointsObject[i].transform.position);
    }

    lineRenderer.positionCount = wayPointsPositions.Count;
    lineRenderer.SetPositions(wayPointsPositions.ToArray());
}

CodePudding user response:

Unity serializes public members by default as they were already open to public. But when you make them private, you say this member is not open to outside.

Unity doesn't serializes private members because setting them may cause unwanted behaviours as you intented it should be private.

But what if you want set a private member? There's a attribute called SerializeField which forces even though it's private but still private to other classes if they acces that class.

CodePudding user response:

I assume you were getting NullReferenceException because the list was not being initialized when it was private.

However, when the list was public, Unity by default serializes public variables to be visible in the inspector, which consequently initializes the list.

This can be fixed by simply initializing the private list to equal new List<Vector3>() on the same line it is declared.

Change this:

private List<Vector3> wayPointsPositions;

To this:

private List<Vector3> wayPointsPositions = new List<Vector3>();

This is what your new code should look like:

private LineRenderer lineRenderer;
private GameObject[] wayPointsObject;
private List<Vector3> wayPointsPositions = new List<Vector3>();

void Start()
{
    lineRenderer = GetComponent<LineRenderer>();
    wayPointsObject = GameObject.FindGameObjectsWithTag("WayPoints");
    for (int i = 0; i < wayPointsObject.Length; i  )
    {
        wayPointsPositions.Add(wayPointsObject[i].transform.position);
    }

    lineRenderer.positionCount = wayPointsPositions.Count;
    lineRenderer.SetPositions(wayPointsPositions.ToArray());
}

Hope this fixes your issue :)

SIDE NOTE - IF YOU WANT TO IMPROVE EFFICIENCY OF YOUR CODE:

Your code can be rewritten without using a list at all, as arrays can be used instead:

The below code has also renamed wayPointsObject to wayPointObjects, wayPointsPositions to wayPointPositions, and the tag "WayPoints" to "WayPoint", as these names make more sense.

private LineRenderer lineRenderer;
private GameObject[] wayPointObjects;
private Vector3[] wayPointPositions;

void Start()
{
    lineRenderer = GetComponent<LineRenderer>();
    wayPointObjects = GameObject.FindGameObjectsWithTag("WayPoint");
    wayPointPositions = new Vector3[wayPointObjects.Length];

    for (int i = 0; i < wayPointObjects.Length; i  )
    {
        wayPointPositions[i] = wayPointObjects[i].transform.position;
    }

    lineRenderer.positionCount = wayPointPositions.Length;
    lineRenderer.SetPositions(wayPointPositions);
}
  • Related