- Home /
UnityException: Transform child out of bounds error when using 2 prefabs in tile manager script.
I am building the endless runner type of game.Unity Exception: Transform child out of bounds error when using 2 prefabs in tile manager script.Please help.The code is as follows.Please check the hierarchy from the following images.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TileManager: MonoBehaviour {
public GameObject[] tilePrefab; public GameObject currentTile;
private static TileManager instance;
public static TileManager Instance { get { if (instance == null) { instance = GameObject.FindObjectOfType < TileManager > (); }
return instance; } }
void Start() { for (int i = 0; i < 10; i++) {
SpawnTiles(); } }
// Update is called once per frame void Update() {
}
public void SpawnTiles() {
int randomIndex = Random.Range(0, 2);
currentTile = (GameObject) Instantiate(tilePrefab[randomIndex], currentTile.transform.GetChild(0).transform.GetChild(randomIndex) .position, Quaternion.identity);
} }