получить происхождение вашего прямоугольного спрайта:
Vector2 rectPos = myrectangle.transform.position;
Получите половину ширины / высоты вашего спрайта:
float rectHeight = myrectangle.GetComponent<SpriteRenderer>().sprite.bounds.extents.y;
float rectWidth = myrectangle.GetComponent<SpriteRenderer>().sprite.bounds.extents.x;
получить случайную позицию в пределах границ;
float xpos = rectPos + Random.Range(-rectWidth, rectWidth);
float ypos = rectPos + Random.Range(-rectHeight, rectHeight);
затем установите положение экземпляров объектов с этими компонентами:
enemyPrefab.transform.position = new Vector2(xpos, ypos);