CodeBlanks Logo Icon CodeBlanks

Primitive objects

Learn primitive objects like Cube, Sphere and Capsule.

What Are Primitive Objects?

To rapidly build prototypes or test environments, Unity provides ready-made basic 3D geometries (primitive objects). These include Cube, Sphere, Capsule, Cylinder, and Plane shapes. They can be created dynamically from code using the GameObject.CreatePrimitive method.

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

Components of Primitive Objects

When a primitive object is created, Unity automatically adds four essential components: Transform for positioning, MeshFilter for geometry, MeshRenderer for drawing, and a Collider (such as BoxCollider) for collisions. Thus the object is immediately visible and ready to interact with the physics engine.

GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Debug.Log(sphere.name); // "Sphere"

Practice Create Cube Primitive

Create a cube using GameObject.CreatePrimitive(PrimitiveType.Cube). Print the object's name using Debug.Log(kup.name);.

Starter code

Player.cs
using System;

public enum PrimitiveType { Cube, Sphere, Capsule, Plane }

public class GameObject
{
    public string name;
    public GameObject(string name) { this.name = name; }
    public static GameObject CreatePrimitive(PrimitiveType type) => new GameObject(type.ToString());
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        // Temel kup nesnesi olustur ve adini ekrana yazdir
    }
}

Sample solution

Player.cs
using System;

public enum PrimitiveType { Cube, Sphere, Capsule, Plane }

public class GameObject
{
    public string name;
    public GameObject(string name) { this.name = name; }
    public static GameObject CreatePrimitive(PrimitiveType type) => new GameObject(type.ToString());
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        GameObject kup = GameObject.CreatePrimitive(PrimitiveType.Cube);
        Debug.Log(kup.name);
    }
}

Expected output

Cube

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which CreatePrimitive call is made to create a Sphere in the scene from code?

  • GameObject.CreatePrimitive(PrimitiveType.Sphere) Correct answer
  • GameObject.CreatePrimitive(PrimitiveType.Cube)
  • GameObject.CreatePrimitive(PrimitiveType.Capsule)
  • GameObject.CreatePrimitive(PrimitiveType.Plane)

Explanation: To create a sphere geometry, the PrimitiveType.Sphere parameter is supplied.

2 Create Capsule Object

Question
GameObject kapsul = GameObject.CreatePrimitive(PrimitiveType.____);
Answer
GameObject kapsul = GameObject.CreatePrimitive(PrimitiveType.Capsule);

Explanation: To create a capsule-shaped primitive, PrimitiveType.Capsule is used.

3 Primitive Types

Term Meaning
PrimitiveType.Cube Cube geometry
PrimitiveType.Sphere Sphere geometry
PrimitiveType.Capsule Capsule geometry
PrimitiveType.Plane Flat ground geometry

All lessons in Intermediate