GameObject class
An object in a World: a Transform, its components (the engine's Camera, Light and MeshRenderer, and any number of the game's Behaviours), and the objects added to it.
Make one, then add it: world.add(GameObject(...)) or
parent.add(child). Until then it only describes itself: its
transform, name and components can be read and changed, and objects
can be added to it, but anything that needs the world (parent,
children, find, worldTransform...) throws a StateError.
Extend it to give an object its own fields and code: a class is what
other engines call a prefab, and each instance is one object. The same
instance comes back from find, children, parent and
World.raycast, so a hit on a character's arm leads to the
Character with its fields.
A subclass is what an object is; a Behaviour is what it does. A
Player, an Enemy or a Level is a subclass: its state, its parts,
and a constructor that makes one. Code another kind of object could use
as well, such as Health, Spin or Flash, is a behaviour, put on
each object that needs it. If a subclass's onUpdate would make sense
on another kind of object, it belongs in a behaviour.
(Called GameObject, not Object, because Dart's Object is every
value's type.)
- Available extensions
Constructors
-
GameObject({String? name, Transform? transform, Iterable<
Component> components = const [], Iterable<GameObject> children = const [], int layer = 0}) -
A new object, not yet in any world, with
transform(the identity if not given),components(as addComponent puts them on) andchildren(as add adds them), so a whole prefab can be one expression:
Properties
- active ↔ bool
-
Whether this object is on. One that is off hides itself and
everything below it: they aren't drawn, don't light the scene or show
it, and aren't hit by rays. Its children keep their own active, so
turning it on again shows the ones that were on. See
activeInHierarchy.
getter/setter pair
- activeInHierarchy → bool
-
Whether this object is shown: it and every object above it active,
and loaded (see GameObject.onLoad).
no setter
- animator → Animator?
-
What plays the model's clips on this copy of it (Animator): for an
object made by Model.instantiate (or Assets.instantiate) from a
model with clips, once it's added; null for any other object, and
for a model's parts.
no setter
- assets → Assets
-
The world's assets, loaded for this object: each one it asks for is
held until it's destroyed, and freed then if no one else holds it.
no setter
- bounds → Bounds?
-
The box in the world round what it and its shown children draw:
each MeshRenderer's Mesh.bounds, turned, scaled and placed as
its object is (Unity's
Renderer.bounds, put together). Its size is a model's size; its Bounds.center lessposition, how far the middle is from the pivot. Null when nothing there draws a mesh. A skinned mesh counts as it is at rest. Worked out each time it's read.no setter - childCount → int
-
How many children it has, read with no list made (Unity's
Transform.childCount): with childAt, to walk them each frame. Before it's added, the children it's been given.no setter -
children
→ List<
GameObject> -
The objects whose parent is this one, in the order they were added.
A new list each time.
no setter
- componentCount → int
-
Available on GameObject, provided by the GameObjectSystems extension
How many components it holds, removed ones among them until the world tidies them away: read them with componentAt, which, unlike GameObject.getAll, makes no list.no setter - debug → Debug
-
The world's World.debug: lines drawn over the scene.
no setter
- forward → Vec3
-
The way the object looks, in the world: its -Z.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- id → int
-
The engine's id: never 0, and never reused. Throws a StateError
before the object is added.
no setter
- input → Input
-
What the player did since the last frame: the world's World.input.
no setter
- isAlive → bool
-
Whether the object is live: added to a world, not destroyed, and the
world not disposed.
no setter
- isDestroyed → bool
-
Whether destroy was called on this object or one above it.
no setter
- layer ↔ int
-
The object's layer, 0 to 31 (Unity's layers): a camera draws only the
layers in its Camera.cullingMask, and a ray (World.raycast) or a
finger (Input.layers) hits only those in its mask. 0 unless set; a
child doesn't take its parent's.
getter/setter pair
-
loaded
→ Future<
void> -
Completes when the object has appeared: it and everything added
during its onLoad are loaded. Fails with the load's error if its
load failed, or with a StateError if it was destroyed first.
no setter
- localPosition ↔ Vec3
-
Where the object is relative to its parent (the world, for a root):
its GameObject.transform's position.
getter/setter pairinherited
- localRotation ↔ Quat
-
How the object is turned relative to its parent.
getter/setter pairinherited
- localScale ↔ Vec3
-
How big the object is relative to its parent. There is no world
scale to set: see
lossyScale.getter/setter pairinherited - lossyScale → Vec3
-
How big the object is in the world: its parents' scales times its
own. Approximate when a parent with an uneven scale has a rotated
child, as Unity's.
no setterinherited
- name ↔ String?
-
The object's name, or null. Names needn't be unique; setting '' is
the same as null.
getter/setter pair
- parent ↔ GameObject?
-
The object this one hangs from, or null for a root. Its transform is
relative to its parent, so it moves, turns and scales with it.
getter/setter pair
- position ↔ Vec3
-
Where the object is in the world, as Unity's
transform.position. Setting it moves the object there, whatever its parents are.localPositionis the same, counted from its parent.getter/setter pairinherited - right → Vec3
-
The object's right, in the world: its +X.
no setterinherited
- rotation ↔ Quat
-
How the object is turned in the world: its parents' rotations and
its own. Setting it turns the object so, whatever its parents are.
localRotationis the same, counted from its parent.getter/setter pairinherited - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- time → Time
-
The world's clock:
time.deltais the seconds this frame counts for. See Time.no setter - transform ↔ Transform
-
Where the object is, how it's turned and how big it is, relative to
its parent:
localPosition,localRotationandlocalScalein one.positionandrotationare the world's. Reading it makes no call into the engine. Setting it checks the value at once, as the engine would, and sends it with the frame's other moves in one call when something next reads the engine's transforms (a view's frame, World.raycast, worldTransform). It reads back exactly as it was set; the engine keeps it in 32-bit floats, so what it draws may differ in the last digits.getter/setter pair - transformStamp → int
-
Available on GameObject, provided by the GameObjectSystems extension
Theworld.transformVersionwhen this object's transform or parent was last set, or 0. While it stays the same, nothing set this object's transform; its world transform also changes when an ancestor's does.no setter - up → Vec3
-
The object's up, in the world: its +Y.
no setterinherited
- world → World
-
The world the object was added to. Throws a StateError before it's
added.
no setter
- worldTransform → Transform
-
Where the object is in the world: its transform after its parents',
worked out in Dart with no call into the engine, and kept until
anything moves. Read only; to move it, set
positionandrotation.no setter
Methods
-
add<
T extends GameObject> (T child) → T -
Adds
childbelow this object and returns it. If this object is live,childis now live too; if not, it becomes live when this one does. -
addComponent<
T extends Component> (T component) → T -
Puts
componenton this object and returns it: a Behaviour (any number), or one of the engine's (at most one Camera, Light and MeshRenderer each). On a live object an engine component is written at once and a behaviour loads (Behaviour.onLoad) and starts the next frame. -
addWith<
T extends Component> (T component, {String? name, Transform? transform, Iterable< Component> components = const [], int layer = 0}) → T -
Adds a new object below this one with
componenton it, and returns the component, for a field:_sun = addWith(Light.sun())whereadd(GameObject(components: [Light.sun()])).get<Light>()!would.name,transformandlayerare the new object's, andcomponentsgo on it aftercomponent, as a camera's rig:addWith(VirtualCamera(), components: [Orbit(player)]). -
ancestor<
T extends GameObject> () → T? -
The nearest object above this one that is a
T, or null. Before it's added, the objects it's been given to count as above it. -
childAt(
int index) → GameObject -
Its child at
index, from 0 to below childCount (Unity'sTransform.GetChild), in children order. -
colorTo(
Color color, {required double seconds, Material? material, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Takes the object's colour to
coloroverseconds(it has nospeed:): seemoveTo. That's its MeshRenderer's own MeshRenderer.color, from the material's if it has none, so the other objects that share the material keep theirs. Givematerialto change that material instead, for everything that uses it. Throws a StateError when there's no renderer with a material.inherited -
componentAt(
int index) → Component -
Available on GameObject, provided by the GameObjectSystems extension
Its component atindex, below componentCount; it may be Component.isRemoved. -
destroy(
{double after = 0}) → void - Removes the object and everything below it. Each gets its onDisable and onDestroy calls first, while it can still be read; using any of them after that throws a StateError saying it was destroyed (isDestroyed tells). Destroying an object again does nothing.
-
find(
String name) → GameObject? -
The first object below this one named
name, searching depth first in children order, or null. This object itself isn't searched. Unlike Unity'sTransform.Find, which looks only at the direct children and takes'a/b'paths, this searches every descendant: find once (in onLoad or onStart) and keep what it gives, rather than each frame. -
get<
T extends Object> () → T? -
The first component on this object of type
T(a class, a subclass, or an interface one implements), or null:get<Health>(),get<Camera>(). Unity'sGetComponent. -
getAll<
T extends Object> () → List< T> -
Every component on this object of type
T, in the order they were put on. A new list each time.Tis a component's type, as for get. -
getAllInChildren<
T extends Object> ({bool includeInactive = false}) → List< T> -
Every component of type
Ton this object and below it, depth first in children order (Unity'sGetComponentsInChildren): to tint every renderer of a model's copy, or stop every particle system under an effect. A new list each time. Objects turned off are skipped unlessincludeInactive, as for getInChildren. -
getInChildren<
T extends Object> ({bool includeInactive = false}) → T? -
The first component of type
Ton this object, or else below it, depth first in children order, or null (Unity'sGetComponentInChildren). Before it's added, the children it's been given count (a model's parts come once it is). -
getInParent<
T extends Object> ({bool includeInactive = false}) → T? -
The first component of type
Ton this object, or else on the nearest object above it that has one, or null (Unity'sGetComponentInParent). Before it's added, the objects it's been given to count as above it. -
has<
T extends Object> () → bool -
Whether this object has a component of type
T, a component's type as for get. -
inverseTransformPoint(
Vec3 point) → Vec3 -
point, given in the world, in the object's own space: the other way fromtransformPoint.inherited -
isPartOf(
GameObject other) → bool -
Whether this object is
otheror below it (Unity'sTransform.IsChildOf, which is also true for itself). Before it's added, the objects it's been given to count as above it. -
localMoveTo(
Vec3 position, {double? seconds, double? speed, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Moves the object to
positionrelative to its parent, aslocalPositionis: seemoveTo.inherited -
localRotateTo(
Quat rotation, {double? seconds, double? speed, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Turns the object to
rotationrelative to its parent, aslocalRotationis: seerotateTo.inherited -
lookAt(
Vec3 point, {Vec3 worldUp = Vec3.up}) → void -
Turns the object to look at
point, in the world, with its up as close toworldUpas it can be. Atpointalready, it has nowhere to look, and stays as it is.inherited -
lookAtOver(
Vec3 point, {Vec3 worldUp = Vec3.up, double? seconds, double? speed, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Turns the object to look at
pointin the world, aslookAtdoes, oversecondsor atspeedradians a second: seemoveTo. Where to look is worked out when it begins.inherited -
moveBy(
Vec3 offset, {double? seconds, double? speed, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Moves the object by
offset, in the world, from wherever it is when it begins: seemoveTo.inherited -
moveTo(
Vec3 position, {double? seconds, double? speed, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Moves the object to
positionin the world overseconds, or atspeedunits a second, from wherever it is when it begins (Unity'sDOMove). The rest is astween's.localMoveTocounts from the parent, so it moves with it.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
on<
T> (Signal< T> signal, void handler(T value)) → SignalSubscription<T> -
Calls
handlerwith each valuesignalemits, until this object is destroyed (with its routines: after its onDisable, before its onDestroy) or the subscription is cancelled. The usual way to listen: a destroyed object never hears anything, and nothing holds it. Whathandlerthrows goes to World.errors. It hears signals while it's hidden too, as in Unity. -
onApplicationPause(
bool paused) → void -
Called when the app goes away (true: to the background, or hidden)
and comes back (false), while the object is shown, as Unity's
OnApplicationPause: to save, as the app may be closed while away. See World.applicationPaused. -
onClick(
Finger finger) → bool - A tap (Input.onTap) on this object or something below it that didn't take it (Finger.over is the hit; Finger.tapCount is 2 for a double tap). Return true to take it; false (the default) passes it to its behaviours, then to the parent.
-
onDestroy(
) → void - Called when the object, or one above it, is destroyed: children before parents, while the object is still there to read (where it was, what it carried). Adding to it then throws.
-
onDisable(
) → void - Called each time a shown object is hidden: it or one above it is turned off, it's moved below a hidden one, or it's destroyed (right before onDestroy). Parents come first, each before its behaviours.
-
onDrag(
Finger finger) → void - The drag this object took moved: Finger.delta is the frame's move.
-
onDragEnd(
Finger finger) → void - The drag this object took ended.
-
onDragStart(
Finger finger) → bool - A drag started from this object or something below it (Finger.startedOver). Return true to take it: this object then gets the drag's onDrag and onDragEnd, wherever the finger goes. False (the default) passes it on, as onClick does.
-
onEnable(
) → void - Called each time the object is shown: when it appears (right after its load, with everything added during it), and when it, or one above it, is turned on (active) or it's moved out from below a hidden one. Parents come first, each before its behaviours.
-
onFixedUpdate(
) → void -
Called every fixed step (Time.fixedDelta, 50 a second by default)
while the object is shown, parents before children, before the
frame's onUpdate: for what must not depend on the frame rate, such as
forces on a rigidbody. A frame runs as many steps as its time holds,
maybe none; inside one,
time.deltais Time.fixedDelta. -
onLateUpdate(
) → void - Called every World.update after every object's onUpdate, in the same order: for what follows where the others went, such as a camera.
-
onLoad(
) → FutureOr< void> - Called once, when the object is added: where it gets its assets and adds its children. Until it, and everything added during it, are loaded, the object is hidden: not drawn, not hit, not started or updated. Then it appears whole, and loaded completes.
-
onLongPress(
Finger finger) → bool - A long press (Input.onLongPress) on this object or below; bubbles as onClick does.
-
onPointerDown(
Finger finger) → bool - A finger went down over this object or something below it that didn't take it. Return true to take it: this object then gets its onPointerUp. False (the default) passes it to its behaviours, then to the parent.
-
onPointerEnter(
Finger finger) → void - A finger came over this object or something below it: the mouse hovering, or a touch. Each object it enters gets it, from the top down, as in Unity.
-
onPointerExit(
Finger finger) → void - A finger is no longer over this object or anything below it (it moved off, went up, or left the view).
-
onPointerUp(
Finger finger) → void - The finger whose onPointerDown this object took went up, wherever it is.
-
onStart(
) → void - Called once, in the first World.update after the object is added and shown, before its first onUpdate. Its parent's comes first.
-
onUpdate(
) → void -
Called every World.update, parents before children, while the
object is shown. Override it to move, animate or think, by
Time.delta (
time.delta); a mixin that overrides it callssuper.onUpdate()so the next one runs too. -
punchScale(
Vec3 punch, {required double seconds, int vibrato = 6, double delay = 0, int loops = 1, bool unscaled = false, void onComplete()?}) → Motion -
Swells the object's scale by
punchand springs it back,vibratoswings that fade overseconds, ending at the scale it began with: a hit, or a pickup.punchScale(Vec3.one * 0.3, seconds: 0.4). It takessecondsonly, nospeed:. One given while another is still punching the object springs about, and ends at, the scale the first began with, so a punch every shot never grows the object.inherited -
release(
{double after = 0}) → void -
Gives this object back to the Pool it came from (Pool.release),
or destroys it if it didn't come from one: what a pooled object calls
where it would call destroy. Releasing it again before it's handed
out again does nothing, as destroying again does. With
after, it's released that much later, as destroy does: unless it's released sooner, so its next use isn't cut short. -
removeComponent(
Component component) → void -
Takes
componentoff this object. A behaviour gets Behaviour.onDisable if it was enabled, then Behaviour.onDestroy, and no calls after; an engine component is taken out of the engine. It can't be put on again. Throws an ArgumentError if it isn't on this object. -
rotate(
Vec3 axis, double angle, {Space relativeTo = Space.self}) → void -
Turns the object
angleradians aroundaxis: an axis of its own by default (rotate(Vec3.up, a)turns it about its own up), or of the world's withrelativeTo: Space.world.inherited -
rotateBy(
Vec3 axis, double angle, {double? seconds, double? speed, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Turns the object
angleradians aroundaxis(one of its own, asrotate's), any number of turns, oversecondsor atspeedradians a second. A coin that spins for ever:rotateBy(Vec3.up, 2 * pi, seconds: 1, ease: Ease.linear, loops: -1).inherited -
rotateTo(
Quat rotation, {double? seconds, double? speed, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Turns the object to
rotationin the world the short way round, oversecondsor atspeedradians a second: seemoveTo.localRotateTocounts from the parent.inherited -
scaleTo(
Vec3 scale, {double? seconds, double? speed, EaseFunction ease = Ease.outQuad, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Sizes the object to
scalerelative to its parent, aslocalScaleis (Unity'sDOScale): seemoveTo.inherited -
sequence(
Iterable< Motion> motions, {double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Plays
motionsone after another: each starts where the last left what it moves. They must be made in this frame, as the arguments of this call usually are, and none may loop for ever. The sequence is a Motion of its own, with its owndelay,loops,yoyo(plays them all back, the last first) andonComplete:inherited -
setParent(
GameObject? parent, {bool worldPositionStays = true}) → void -
Hangs this object from
parent, last among its children; null makes it a root. WithworldPositionStays(as Unity'sSetParent) it stays where it is in the world, turned and sized as it was, so its transform changes; without, its transform stays, now counted from the new parent, so it moves with it. A parent that is this object or below it throws an ArgumentError, as does one from another world. -
shake(
double strength, {double seconds = 0.4, double frequency = 20, int? seed, double delay = 0, int loops = 1, bool unscaled = false, void onComplete()?}) → Motion -
Shakes the object about where it is, up to
strengthalong each axis,frequencytimes a second, fading overseconds(0.4 unless given, as Cameras.shake) and ending where it began.seedmakes the same shake each time. It takessecondsonly, nospeed:.strengthandfrequencyare 0 or more. One given while another is still shaking the object shakes about, and ends at, where the first began.inherited -
startRoutine<
T> (Future< T> body()) → Routine<T> -
Starts a routine: runs
bodynow, up to its first wait, and returns the Routine, which can be awaited for whatbodyreturns, or stopped. Its waits (nextFrame, wait, waitUntil, waitWhile, race, waitAll) end in the routines step of a later frame, after every onUpdate (docs/stage-4.md ยง3.2): -
stopAllRoutines(
) → void -
Stops every routine this object started (startRoutine), not those
its behaviours did (Unity's
StopAllCoroutines). -
together(
Iterable< Motion> motions, {double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Plays
motionsall at once, as one Motion that lasts as long as the longest: seesequence.inherited -
toString(
) → String -
Its class and id, and its name if it has one:
Player#7,GameObject#5 'door',GameObject 'door' (not added).override -
transformPoint(
Vec3 point) → Vec3 -
point, given in the object's own space, in the world's: where a point on it is.inherited -
translate(
Vec3 offset, {Space relativeTo = Space.self}) → void -
Moves the object by
offset: along its own axes by default (translate(Vec3.forward)moves it a metre the way it looks, its scale aside), or the world's withrelativeTo: Space.world.inherited -
tween<
T> (T from, T to, {required void onUpdate(T value), double? seconds, double? speed, EaseFunction ease = Ease.outQuad, T lerp(T a, T b, double t)?, double delay = 0, int loops = 1, bool yoyo = false, bool unscaled = false, void onComplete()?}) → Motion -
Takes a value from
fromtotooverseconds, or atspeedunits a second (give one), callingonUpdatewith it each frame:inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited