Help assigning transform values to a Vector3 array

Hi, I am trying to make a game that requires the instantiating of multiple objects behind one another, and then for those objects to take the exact same path as the object in front of it. If that didn’t make any sense, think snake gameplay.

At the moment I have a script attached to my player gameObject which is the head of the ‘snake’. I can assign the head transform correctly to the Vector3 array, however when I try to assign the transform’s of the body parts to the array nothing happens.

From what I can see in my code, and what I have seen in examples online it should be working.

Just ignore the newPositions array, I thought I was going to need it and haven’t removed it yet just in case I do need it.

Here is my code:

#pragma strict

var snakeSize = new GameObject[5];
var oldPositions = new Vector3[6];
var newPositions = new Vector3[6];
var body : GameObject;

function Start () {
	
	//Assign body parts to the array
	for (var x : int = 0; x < snakeSize.Length; x++){
		
		snakeSize[x] = body;
	}
	
	//Instantiate all the body parts from the array
	for (var a : int = 0; a < snakeSize.Length; a++){
	
		Instantiate(snakeSize[a], Vector3(transform.position.x, transform.position.y - a/2, transform.position.z), transform.rotation);
	}
}

function Update () {
	
	//Store old positions of head and body parts
	oldPositions[0] = transform.position;
	
	for (var i : int = 0; i < snakeSize.Length; i++){
	
		oldPositions[i+1] = snakeSize*.transform.position;*
  • }*

  • //Move body parts to the old position of the body part infront*

  • snakeSize[0].transform.position = oldPositions[0];*

  • for (var b : int = 1; b < snakeSize.Length - 1; b++){*

snakeSize__.transform.position = oldPositions**;
}**__

}
Any help would be greatly appreciated.

Ok so your problem is you are happily filling your array of snakeSize parts with a pointer to the one prefab, and not the instantiated part you are putting there. Hence all your loop is doing is moving the invisible prefab!

 snakeSize[a] = Instantiate(body, Vector3(transform.position.x, transform.position.y - a/2, transform.position.z), transform.rotation);

Should help.