private void spawnEnemy() { GameObject newEnemy = spriteHandler.GetNewEnemy(edginessHandler.getEdges()); if (newEnemy != null) { int index = Random.Range(0, nPos); Vector3 spawnPos = spawnPositions[index].position; Vector3 playerPos = spriteHandler.transform.position; while (Vector3.Distance(new Vector3(spawnPos.x, spawnPos.y, 0), new Vector3(playerPos.x, playerPos.y, 0)) < spawndistance) { index = Random.Range(0, nPos); spawnPos = spawnPositions[index].position; } Instantiate(newEnemy, spawnPositions[index].position, Quaternion.identity); } Invoke("spawnEnemy", spawnRate); }