Draw a line from gameobject at end of the array to the gameobject in the start of the array

Hi all,

I have got line renderer drawing a line through all game objects in an array. However, I would like to draw the line renderer from the game object at the end of the array back to the game object at the start of the array. All game objects that I am drawing a line through are instantiated objects. This code below draws a line renderer from game object at start of array to the game object stored at the end of the array. Any help is appreciated.

What I am doing so far:

public void MeasureArea()
    {
        GameObject[] points = GameObject.FindGameObjectsWithTag("MeasuringPoint");

        lrA = GetComponent<LineRenderer>();
        lrA.material = new Material(Shader.Find("Transparent/Diffuse"));
        lrA.startWidth = 0.1f;
        lrA.endWidth = 0.1f;
        pointLine = new Vector3[points.Length];


        int n = points.Length;


        for (int i = 0; i < n - 1; i++)
        {
            pointLine *= points[i % n].transform.position;*

Debug.Log(points.Length);
}

lrA.numPositions = pointLine.Length;
lrA.SetPositions(pointLine);

3 Answers

3

You need to pass your first gameobject as the last item in your array.

public void MeasureArea()
{
	GameObject[] points = GameObject.FindGameObjectsWithTag("MeasuringPoint");

	lrA = GetComponent<LineRenderer>();
	lrA.material = new Material(Shader.Find("Transparent/Diffuse"));
	lrA.startWidth = 0.1f;
	lrA.endWidth = 0.1f;
	
	int n = points.Length + 1;
	pointLine = new Vector3[n];

	for (int i = 0; i < n - 1; i++)
	{
		pointLine _= points*.transform.position;*_

* Debug.Log(points.Length);*
* }*

* // Just push first element at the last position.*
* pointLine[n-1] = points[0];*

* lrA.numPositions = n;*
* lrA.SetPositions(pointLine);*
}

Thanks for input, however when I instantiate 4 points (game objects) only 3 points connect as required but the 4th point is all on its own with no lines. :S

Thanks for input, however when I instantiate 4 points (game objects) only 3 points connect as required but the 4th point is all on its own with no lines. :S

@HarshadK This worked however I have an extra game object that isn’t connected with the lines. e.g I placed 4 instantiated objects in the scene, but only lines are drawn from 3 points making a triangle. I am looking to draw lines between all game objects into the array, when at end of array it will draw a line to the gameobject at the start of the array. This was good but as mentioned above it left out a game object to form a triangle instead of a square.

I made a change in code pointLine[n-1] = points[0]; to pointLine[n - 1] = pointLine[0] as it gave a compiler error " Cannot implicitly convert type ‘UnityEngine.GameObject’ to UnityEngine.Vector3’.