GameObject List all elements getting replaced

I am using this code:

import System.Collections.Generic;

public var waypoint : GameObject;
public var waypointArray = new List.<GameObject>();
private var canCallFunction: boolean = true;
private var grounded:boolean = false;
var count=0;    
var waypointPos:Vector3;

function Start () {
}
function Update () {
if (grounded){
if(canCallFunction){count++;
Wait(5);
Instantiate(waypoint,this.transform.position, this.transform.rotation);
waypoint.transform.localPosition = this.transform.position;

waypointArray.Add(waypoint.gameObject);
for (var i:int=0;i<waypointArray.Count;i++){
var temp:GameObject = waypointArray*;*

Debug.Log(i+" : "+temp.transform.position);
}}
}
function Wait(time : float){
canCallFunction= false;
yield new WaitForSeconds(time);
canCallFunction= true;
}

function OnCollisionEnter(theCollision:Collision)
{
if (theCollision.gameObject.name == “GroundCube”)
{
grounded=true;
}
}
But the values I am getting with the Debug are all the same when they should be the same depending on the movement. Why is this?

What you are doing is changing the position of the prefab (not the newly created object), and you are inserting the prefab into the array. Your array entries are ‘by reference’ meaning that each entry is a pointer to the object, not a copy of the object. That means you are filling your array with pointers all referencing the prefab. Since all the entries in the array are pointing to the same object, when you change that object, the output changes for all entries in the array. I think you may want something like this:

import System.Collections.Generic;
 
public var waypoint : GameObject;
public var waypointArray = new List.<GameObject>();
private var canCallFunction: boolean = true;
private var grounded:boolean = false;
var count=0;    
var waypointPos:Vector3;
 

function Update () {
	if (grounded){
		if(canCallFunction){
			Debug.Log("Here");
			count++;
			Wait(5);
			var go = Instantiate(waypoint,transform.position, transform.rotation);
			//go.transform.localPosition = this.transform.position;
			 
			waypointArray.Add(go);
			for (var i:int=0;i<waypointArray.Count;i++){
				var temp:GameObject = waypointArray*;*
  •  		Debug.Log(i+" : "+temp.transform.position);*
    
  •  	}*
    
  •  }*
    
  • }*
    }

function Wait(time : float){
canCallFunction= false;
yield new WaitForSeconds(time);
canCallFunction= true;
}

function OnCollisionEnter(theCollision:Collision)
{
if (theCollision.gameObject.name == “GroundCube”)
{
grounded=true;
}
}
As a separate potential issue, once your ‘grounded’ is set to true, it stays true…there is no code to return it back to false.