Instantiating 3D Objects in Unity3D

In many ways, 3D game development is technically more complex than than 2D development, in animations, object designs and scene layout for examples. However, in programming, one can still find many common grounds in the techniques deployed. I have 2 working examples here illustrating the spawning methods applied in similar fashion for both 2D and 3D platforms. The targeted number of spawned objects are instantiated at the first instance, countered checked and cleared for overlapping with iterative, nested loops. By large, the basic algorithm remains intact and the objectives of the various methods on object spawning, random generation of vector positions yield similar results. The necessary adaptive changes are applied in the construction of the parent containers, from a 2D UI Panel to a 3D cube in addition to the treatments of the Z-axis values in spawned items.

Differences between 2D and 3D Instantiations albeit the Similarities in  Algorithm: 

  • The methods  used for computing the container’s area in 2D and the container’s volume in 3D. The containers are the parents and the spawned objects are referred to as the children in Unity. In 2D, the parent container I used in an earlier example is a Panel, a user interface (UI) element and like all other UI elements, it comes with a the Rect Transform component, represented by a rectangle, a 2D layout where a UI element can be placed inside. The area of this rectangle is calculated from the minimum and maximum values of the x and y coordinates. In 3D, the volume of the cube in this case is calculated from the Box Collider component, defined as the shape of an object for the purposes of physical collisions. The size in volume of the Collider is computed from its X, Y, Z directions.
  • The critical ‘Z’ values. We have to flatten and fixed the Z-axis value for 2D spawning to represent a flat plane from the camera’s perspective. In 3D, the Z-axis value is randomly generated like the X and Y axes values. In both cases, we use a method call (see below, GetRandomVector3() method) to return a randomly generated position in vector 3 so adjusting for differences in calculation doesn’t change the basic algorithm of the spawning process.
  • Background and Skybox Scenes. In 2D, the entire scene and the major parts of game play reside inside a Canvas. This can be easily done by attaching an image component to the Canvas and pull in a 2D graphic. In 3D, the skybox acts as the scene wrapper. Skybox images are created from a series of high quality HDRI environment maps like these. These are complimentary design considerations.

Computations are done in the Start() and GetRandomVector3() methods in both the 2D and 3D applications.
Coding: 3D Volume from the Box Collider Component

void Start()
        parentContainer.transform.position =;
        //Fetch the Collider from the GameObject
        cubeCollider = parentContainer.GetComponent();

        //Fetch the size of the Collider volume
        cubeColliderSize = cubeCollider.bounds.size;

        minXContainer = -cubeColliderSize.x / 2 + padding;
        minYContainer = -cubeColliderSize.y / 2 + padding;
        minZContainer = -cubeColliderSize.z / 2 + padding;
        maxXContainer = cubeColliderSize.x / 2 - padding;
        maxYContainer = cubeColliderSize.y / 2 - padding;
        maxZContainer = cubeColliderSize.z / 2 - padding;


    //Get random x,y and z for Vector3
    private Vector3 GetRandomVector3()
        float randomX = Random.Range(minXContainer, maxXContainer-1);
        float randomY = Random.Range(minYContainer, maxYContainer-1);
        float randomZ = Random.Range(minZContainer, maxZContainer-1);
        Vector3 v3 = new Vector3(randomX, randomY, randomZ);

        return v3;

Application: 3D Random Spawning

Coding: 2D Area from the Rect Transform Component

    [Header("Parent Container Parameters")]
    private Rect rectParentContainer;
    public float minXContainer;
    public float minYContainer;
    public float maxXContainer;
    public float maxYContainer;
    public float fixedY = -10;
    public float padding = 30;

     private void Start()
        rectParentContainer = parentContainer.GetComponent().rect;
        minXContainer = rectParentContainer.xMin+padding;
        minYContainer = rectParentContainer.yMin + padding;
        maxXContainer = rectParentContainer.xMax - padding;
        maxYContainer = rectParentContainer.yMax - padding;


    //Get random x,y for Vector3 but fixed z
    private Vector3 GetRandomVector3()
        float randomX = Random.Range(minXContainer, maxXContainer);
        float randomY = Random.Range(minYContainer, maxYContainer);

        Vector3 v3 = new Vector3(randomX, randomY, fixedY);

        return v3;

Application: 2D Random Spawning

Leave a Reply

Your email address will not be published. Required fields are marked *