I have 3 objects.
PlatformManager
Platform
Tile
My Tile prefab is just a sprite and a Box Collider2D
My Platform prefab is generated from n tile prefabs. Eg. 3 tiles.
PlatformManager spawns platforms on a timer as children and moves to the left.
If I Instantiate a Tile prefab directly, I can set it’s position to that of a GameObject parented to the PlatformManager named spawnPoint.
public GameObject tile;
public Transform spawnPoint;
void Start() {
Instantiate(tile, spawnPoint.position, Quaternion.identity, gameObject.transform);
}
That works as expected. The tile prefab appears positioned at the spawnPoint object’s position. But if I use the same approach with the Platform:
public GameObject platform;
public Transform spawnPoint;
void Start() {
Instantiate(platform, spawnPoint.position, Quaternion.identity, gameObject.transform);
}
The Platform prefab instantiates at the world origin.
This is the platform generation code:
public class PlatformGenerator : MonoBehaviour
{
public int platformSize; //in tiles
public GameObject tile;
public float tileOffset;
Transform currentOffset;
Transform myTrans;
void Start()
{
for (int i =0; i < platformSize; i++)
{
Instantiate(tile, new Vector3(i * tileOffset, 0, 0), Quaternion.identity, gameObject.transform);
}
}
}
Any ideas?