void Generate() { int currentTotalLength = 0; int zoneLength = 0; for (int i = 0; i < totalZoneAmount; i++) { if (i % 2 == 0) { zoneLength = UnityEngine.Random.Range(minSafeZoneLength, maxSafeZoneLength + 1); float initialZ; if (i == 0) { initialZ = firstZoneZ; zoneLength++; currentTotalLength--; } else { initialZ = initialZoneZ + currentTotalLength; if (i == totalZoneAmount - 1) { zoneLength += lastZoneExtraLength; } } zones.Add(Instantiate(safeZonePrefab, transform)); SafeZone newZone = zones[zones.Count - 1].GetComponent <SafeZone>(); if (newZone) { newZone.Initialize(initialZoneX, initialZ, levelWidth, zoneLength); } } else { zoneLength = UnityEngine.Random.Range(minBaseDangerZoneLength, maxBaseDangerZoneLength + 1) + currentLevel; DangerZoneSO newZoneSO = dangerZoneSOs[UnityEngine.Random.Range(0, dangerZoneSOs.Count)]; zones.Add(Instantiate(newZoneSO.prefab, transform)); DangerZone newZone = zones[zones.Count - 1].GetComponent <DangerZone>(); if (newZone) { newZone.Initialize(initialZoneX, initialZoneZ + currentTotalLength, levelWidth, zoneLength); newZone.SetType(newZoneSO.type); newZone.SetObstacleValues(newZoneSO.minObstacleGenerationTime, newZoneSO.maxObstacleGenerationTime, newZoneSO.minObstacleSpeed, newZoneSO.maxObstacleSpeed); newZone.SetListElements(newZoneSO.tilePrefabs, newZoneSO.obstaclePrefabs); if (newZoneSO.type == DangerZoneTypes.Water) { newZone.SetWaterZoneTriggerPrefab(waterZoneTriggerPrefab); } } } currentTotalLength += zoneLength; } GameObject levelEndTrigger = Instantiate(levelEndTriggerPrefab, transform); Vector3 size = levelEndTrigger.GetComponent <BoxCollider>().size; size.x = levelWidth; levelEndTrigger.GetComponent <BoxCollider>().size = size; Vector3 position = levelEndTrigger.transform.position; position.z = currentTotalLength - zoneLength - 1f - tileOffset; levelEndTrigger.transform.position = position; }