Adding gameobjects to an existing array.

Hello!

I am creating a graph using Debug.Drawline. What i want to do is add additional gameobjects to the existing array.

This is my script:

public GameObject point;
	public GameObject[] points;

	void Start () {

		//First line
		for (int i = 0; i < 11; i++){
			x = i;
			y = Random.Range(0f,1f);

			points *= Instantiate(point,new Vector3 (x,y,0),transform.rotation) as GameObject;*
  •  }*
    
  • }*
    The new gameobjects should be placed at x = 0.5, 1.0, 1.5…
    for the y coordinate the first new gameobjects should be placed at:
    if (points*.tranform.position.y > points[i+1].tranform.position.y)*
    new object.transform.position.y = points_.tranform.position.y - abs(points*.tranform.position.y-points[i+1].tranform.position.y)
    else
    new object.transform.position.y = points.tranform.position.y + abs(points.tranform.position.y-points[i+1].tranform.position.y)
    ^is easy, what i cant understand is how to put these new objects in points array.
    I made a picture below:
    [60023-unityhelp.png*
    |60023]*

    *
    All help is appretiated!
    Thank you!
    /Taegos_

If you want to add elements dynamically you should use a List instead of an array. Here you have a tutorial about lists and dictionaries. Remember to add using System.Collections.Generic;

Check this:

public List<GameObject> points = new List<GameObject>();

void Start()
{
    //First line
    for (int i = 0; i < 11; i++)
    {
        x = i;
        y = Random.Range(0f,1f);
 
        points.Add(Instantiate(point,new Vector3 (x,y,0),transform.rotation) as GameObject);
    }
}

If you still want to use an array, you can fill a List and convert it to an array with List.ToArray() method. But take in mind GC, as you will be allocating memory every time you call this method.