Menu
CoddyTech

Place the Cube

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 2 of 67.

Every GameObject has a Transform: its position, rotation and scale. A script reaches its own object's Transform as transform, and transform.position is where the object's center is, as a Vector3: three numbers for x (right), y (up) and z (forward, away from the camera). One unit is one meter.

To put an object somewhere, give it a new position in Start(). This lamp is 2 units tall and should stand on a table whose top is at y = 2, centered at x = -2, z = 4. The lamp's center goes half its height above the table top, so its y is 2 + 1 = 3:

void Start()
{
    transform.position = new Vector3(-2, 3, 4);
}

While the game plays, select the lamp in the Hierarchy: the Inspector's Transform shows the same three numbers as its Position.

challenge icon

Challenge

Easy

The orange platform is centered at x = 3, z = 2, and its top is at y = 1.5. The blue cube is 1 unit tall. In Start(), set the cube's position so it rests on the middle of the platform.

Remember that a position is the object's center, not its bottom.

Press Play to see where it lands, then Check.

Try it yourself

using UnityEngine;

public class PlaceCube : MonoBehaviour
{
    void Start()
    {
        // x, y, z: change these three numbers
        transform.position = new Vector3(0, 1, 0);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unity Scripting Basics