I'm trying to create a infinite racing game. I have a RoadSystem game object that instantiates a slab of road. On awake about 8 slabs are created. I'm trying to create a system where once the car passes the second or the third slab, the first slab is destroyed and then a new one is added at the end. I'm unable to get the index of the slabs. This is the code I have attached to a trigger which is just an empty gameobject with a box collider attached:
public class AddRemoveRoadSlab : MonoBehaviour {
Public Road road
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "RoadSlab"){
Debug.Log(road.GetComponent<Transform>().GetSiblingIndex());
}
}
What the debug.log statement is printing is the index of the Road gameobject in the Hierarchy that includes the camera, direction light etc. I want to get the index of the children of the Road gameobject. Can someone please help?
EDIT
Here is a screenshot of how the Road and slab are configured. The road script generates the slabs. The slabs are just unity plane objects.
other
argument? \$\endgroup\$