Game object instantiated and added to list but the change in capacity shows that 4 game objects have been added to the list?

Pretty much as the title says. So my list capacity was not adding up when i checked it at the end of my code, but seemed fine in the inspector, so i broke it down and found an anomaly where my list capacity doubled. So first i made it where in the code above i instantiated an the object not to be the same object instantiated again (hence empty2). Did not work. So i thought that the instantiation duplication may be due to both the object being exactly the same, empty game objects, so i tagged empty2 with a new tag of empty 2. Still did not work! So here i am asking you if you know what is happening here. Thank you for any help. Apologies in advance for the messiness of my code (below).

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class PolyDrawExample : MonoBehaviour
    public int numberOfSides;
    public float polygonRadius;
    public Vector2 polygonCenter;
    public GameObject empty;
    public GameObject empty2;
    List<GameObject> Lines;
    Line Line;
int i;
List<Vector3> Vertices;
Vector2 currentCorner;
public GameObject button;
public GameObject buttonObj;
new public Camera camera;
public Transform canvas;
Vector3 buttonPressed;
int i2;
List<GameObject> Buttons;
int i3;

void Start()
    numberOfSides = 5;
    polygonRadius = 2.5f;
    i = 1;
    polygonCenter = new Vector2(0, 0);
    Lines = new List<GameObject>();
    Vertices = new List<Vector3>();
    Buttons = new List<GameObject>();
    Line = GetComponent<Line>();
    DrawPolygon(polygonCenter, polygonRadius, numberOfSides);

void Update()
    i2 = 0;
    while (numberOfSides + 1 > i2)
        if (Buttons[i2].GetComponent<ButtonPressControl>().click == true)
            Debug.Log(Buttons[i2].name + " is pressed");
        i2 = i2 + 1;


void DrawPolygon(Vector2 center, float rad, int numSides)
    Vector2 startCorner = new Vector2(rad, 0) + polygonCenter;
    Vector2 previousCorner = startCorner;

    for (i = 1; i < numSides; i++)
        float cornerAngle = 2f * Mathf.PI / (float)numSides * i;
        currentCorner = new Vector2(Mathf.Cos(cornerAngle) * rad, Mathf.Sin(cornerAngle) * rad) + polygonCenter;
        // create empty game object
        GameObject lin = Instantiate(empty) as GameObject;
        // name it with number = "Line " + i;
        // add to lines list
        // create new line (made class in Line). Attach game object and corners and colours
        Line.line line = new Line.line(Lines[i - 1], currentCorner, previousCorner,;
        // attach the linerenderer to game object with said info from above

        // Having used the current corner, it now becomes the previous corner.
        previousCorner = currentCorner;

        Vertices.Insert(i - 1, previousCorner);
    // lines list capacity is 4
    // Draw the final side by connecting the last corner to the starting corner.
    GameObject li = Instantiate(empty2) as GameObject; = "Line " + i;
    // is 4 as should be      LOOK HERE    LOOK HERE    LOOK HERE   LOOK HERE   LOOK HERE - obvious enough i hope... :(
    // is 8???               LOOK HERE    LOOK HERE   LOOK HERE   LOOK HERE   LOOK HERE
    Line.line linee = new Line.line(Lines[i - 1], startCorner, previousCorner,;
    Vertices.Insert(i - 1, startCorner);

    GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    sphere.transform.position =;
    sphere.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
    sphere.GetComponent<Renderer>().material = new Material(Shader.Find("GUI/Text Shader"));
    sphere.GetComponent<Renderer>().material.color =;
    i = 1;

    //Creates center lines
    while (i < numSides + 1)
        GameObject li2 = Instantiate(empty) as GameObject; = "Line " + (i + numberOfSides);
        Line.line linee2 = new Line.line(Lines[i + numSides - 1],, Vertices[i - 1],;
        i = i + 1;
    i = 1;
    while(i < numSides + 1)
        GameObject sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere2.transform.position = Vertices[i - 1];
        sphere2.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        sphere2.GetComponent<Renderer>().material = new Material(Shader.Find("GUI/Text Shader"));
        sphere2.GetComponent<Renderer>().material.color =;
        i = i + 1;

    i = 1;
    while (i < numSides + 2)
        GameObject Button = Instantiate(button) as GameObject;
        Button.transform.SetParent(canvas, false);
        Button.transform.position = camera.WorldToScreenPoint(Vertices[i - 1]); = "Button " + i;
        i = i + 1;


Ideas of what is causing the problem -

  • As in the inspector there is no other game objects maybe the error relates to the .Add function on the list

A List<> needs to allocate memory in order to use it, but unlike an Array, it doesn’t specifically have to do so all at once.

In exchange for this, Lists allocate chunks of memory at a time in groups, rather than only for each individual element at once.

Therefore, what you’re seeing when querying the List<> is the total memory allocated for the list, or its Capacity. To return the total number of elements you’ve added to your list at present, you would instead use Count.

Edit: In other words, Capacity is the total space available in the List at present, whereas Count is the total space you’ve used of that capacity.