Question by
unity_yc5iJO7rfoRkyA · Dec 16, 2019 at 06:01 PM ·
endless runnerrunner
hey there I wanna know where i m making mistake in this code
I was watching a tutorial on youtube about endless runner and i did as he did in the video but my clone tile is not spawning on correct position here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileManager : MonoBehaviour
{
//for spawn we made an array name of game object
public GameObject[] tilePrefabs;
private Transform PlayerTransform;
private float spawnZ = -6.0f;
private float tilelength = 12.0f;
private float safeZone = 15.0f;
//amount of tiles...
private int amnTilesOnScreen =7;
private List<GameObject> activeTiles;
// Start is called before the first frame update
void Start()
{
activeTiles = new List<GameObject>();
//transform jo k ek inspector window ma as a component work kry ga
PlayerTransform = GameObject.FindGameObjectWithTag("Player").transform;
for(int i=0;i<amnTilesOnScreen;i++)
{
SpawnTile();
}
}
// Update is called once per frame
void Update()
{
if (PlayerTransform.position.z-safeZone > (spawnZ - amnTilesOnScreen * tilelength))
{
SpawnTile();
DeleteTile();
}
}
private void SpawnTile(int prefabIndex = -1)
{
GameObject go;
go = Instantiate(tilePrefabs[0]) as GameObject;
go.transform.SetParent(transform);
go.transform.position = Vector3.forward;
spawnZ += tilelength;
activeTiles.Add(go);
}
private void DeleteTile()
{
//for destroy the tiles
Destroy(activeTiles[0]);
//tiles ki position ki removement krna..
activeTiles.RemoveAt(0);
}
}
Comment