Unity3D menggunakan sistem berbasis komponen secara default. Ini luar biasa untuk membuat entitas game dari file teks dan injeksi ketergantungan.
function createEnemy() {
// extract AI type for enemy
// definition is a custom structure holding parameters to create the enemy
var aitypename = definition.ai;
// AIType can be an interface or abstract class
// you can create a component from a string or from a type
var ai : AIType = this.gameObject.AddComponent(aitypename);
ai.setup(definition.ai_settings);
// set rule for enemy when it is destroyed
this.gameObject.AddComponent(definition.when_destoryed);
}
Komponen-komponen itu bisa terlihat seperti ini
class AI_Scout extends AIType
{
// called per update-frame on the game-object with this script
public function Update() {
// run Scout AI here
}
}
class Spawn_Ammo_On_Destroyed extends When_Destroyed
{
// automatically called by the engine when the game object this script is attached to is
// destroyed
public function OnDestroyed() {
// spawn ammo
}
}