Skip to content

Components

An entity is a bag of components — a sprite, a physics body, an audio source. From a script you reach them through GetComponent<T>().

csharp
public class Player : GameEntity
{
    private Rigidbody2DComponent? m_Body;
    private SpriteRendererComponent? m_Sprite;

    protected override void OnCreate()
    {
        m_Body   = GetComponent<Rigidbody2DComponent>();
        m_Sprite = GetComponent<SpriteRendererComponent>();
    }
}

How the façades work

Every class on this page is a façade: a thin object with no state of its own that reads and writes straight through to the engine's component the moment you touch a property.

Two consequences worth knowing:

  • GetComponent<T>() builds a new façade every call. It's cheap, but not free — cache it in a field in OnCreate rather than calling it in OnUpdate.
  • There are no C# events on components. Nothing native holds a durable reference to a façade, so there is nothing to raise an event on. Notifications are GameEntity virtual methods instead — OnClick, OnAnimatorStateEntered, OnTriggerEnter. See GameEntity.

Checking before you reach

GetComponent<T>() returns null when the entity doesn't have that component. A null-checked call that silently does nothing looks exactly like "the feature is broken", so check explicitly when you're not sure:

csharp
if (HasComponent<Rigidbody2DComponent>())
    GetComponent<Rigidbody2DComponent>()!.LinearVelocity = Vector2.Zero;

The component façades

ClassEditor nameReference
TransformComponentTransformBelow
Rigidbody2DComponentRigidbody 2DRigidbody2DComponent
SpriteRendererComponentSprite RendererSpriteRendererComponent
TextComponentText ComponentTextComponent
CameraComponentCameraCamera
CameraFollowComponentCamera FollowCamera
AnimatorAnimatorAnimator
AudioSourceComponentAudio SourceAudio
ParticleEmitterParticle EmitterParticles
TilemapComponentTilemapTilemapComponent
CanvasCanvasUI
RectTransformRect TransformUI
UIImageUI ImageUI
UIButtonComponentUI ButtonUI
UIProgressBarUI Progress BarUI

Components with no C# façade yet

Several components exist in the editor but have no scripting surface in 0.1.0: Audio Listener, Circle Renderer, Box Collider 2D and Circle Collider 2D (configure them in the Properties panel; query them with Physics2D), and the UI components UI Selectable, UI Slider, UI Toggle, UI Layout Group, UI Mask and UI Scroll Rect. See UI scripting for the workaround.

TransformComponent

The entity's position. There is only one member, and it forwards to GameEntity.Translation — use whichever reads better where you are.

Translation

csharp
GetComponent<TransformComponent>().Translation = new Vector3(0, 5, 0);
// identical to:
Translation = new Vector3(0, 5, 0);

Rotation and scale

There is no C# access to rotation or scale in 0.1.0 — set them in the Properties panel. Position is the only transform channel a script can drive.

See also

  • GameEntityGetComponent, HasComponent, and the lifecycle hooks components notify through
  • Attributes — exposing your own script fields to the Properties panel