Get the transform.position of a gameobject in an array?
Hello!
I am trying to get the position of the last gameobject in an array. What i get is the starting position only, not the updated one that i need. Very strange since i check for the position in the update function:
using UnityEngine;
using System.Collections;
public class TileGeneration : MonoBehaviour {
public GameObject plainTile;
public float x;
public float y;
public GameObject[] startTiles;
public Vector3 lastTilepos;
void Start () {
for (int i = 0; i < 6; i++)
{
startTiles[i] = plainTile;
Instantiate (startTiles[i],new Vector3(x,y,0),transform.rotation);
x = x + 2.4f;
}
}
void Update () {
lastTilepos = startTiles [startTiles.Length - 1].transform.position;
}
}
Any ideas?
Thank you!
To clarify, the plainTile prefab has a movement script attached, so these are all moving platforms.
Answer by ElDo · Oct 31, 2015 at 11:55 AM
Change:
startTiles[i]=plainTile;
Instantiate(startTiles[i],new Vector3(x,y,0),transform.Rotation);
into:
startTiles[i]=Instantiate(painTile,new Vector3(x,y,0),transform.Rotation) as GameObject;
Answer by hexagonius · Oct 31, 2015 at 11:35 AM
In 16 you're assigning the prefab to the array. Then you instantiate it, BUT it's just in the scene and not assigned anywhere (It's a new instance). Instantiate plainTile and assign the returned gameobject to your array, then you're referencing the new instance and not the prefab anymore.
thanks! The other answer gave code examples but this was also sufficent :)