This unity asset instantiates, keeps track of and removes cubes based on player input.
This repository is a drop-in script, not a Unity project. There is no scene, no
Assets/ folder and no package manifest here — only the single MonoBehaviour in
src/.
- Copy
src/CubeFactory.csinto theAssets/folder of an existing Unity project. - Let Unity finish compiling; the
CubeFactorycomponent becomes available.
- Create a cube prefab (for example, from a built-in
CubeGameObject). - Add an empty GameObject to the scene.
- Attach the
CubeFactorycomponent to it. - Assign the cube prefab to the component's Cube Prefab field in the Inspector.
The Cube Prefab field has no default value. If it is left unassigned, pressing
E spawns nothing and writes an error to the Console naming the field to assign.
| Key | Action |
|---|---|
E |
Spawns a cube and adds it to the tracked list |
R |
Destroys one tracked cube, if any remain |
Both keys are handled in Update() and are read once per key press, not per frame
held.
- Every cube is spawned at the world origin,
(0, 0, 0). Spawning several cubes therefore produces a stack of overlapping cubes at the same point rather than a visible spread. Rremoves the oldest cube still tracked, not the most recently spawned one.- Pressing
Rwith no cubes tracked does nothing. - After each spawn and each removal, a labelled message naming the action and the
resulting cube count is written to the Console — for example
CubeFactory: spawned cube, count is now 1. - Pressing
Ewhile Cube Prefab is unassigned logs an error naming the field and leaves the tracked list unchanged.
| Field | Type | Purpose |
|---|---|---|
cubePrefab |
GameObject |
The prefab instantiated on each spawn. Must be assigned. |
cubes |
List<GameObject> |
The cubes currently tracked. Populated at runtime; assigning it in the Inspector is not expected. |
Licensed under the Stephenson Software Non-Commercial License (Stephenson-NC). See LICENSE.