Arma 3 Scripting

Command in Object Manipulation

createUnit

Also available as Markdown and JSON, or read the original wiki page.

Description

Create a unit of the provided CfgVehicles class.

Warning

The unit will not be created if the passed group does not exist (a.k.a grpNull); this can happen if createGroup fails because the group limit has been reached (see createGroup for respective game limits).

#Syntax 1 #Syntax 2
Group's locality the provided group can be non-local, but a warning will be logged the provided group must be local
Unit's side using a classname from a different side than the provided group will result in the unit itself being of a (config-defined) side inside a group of another side - see #Example 6 for more information using a classname from a different side than the provided group will result in the unit being of the same side as the provided group
Other the unit's init code will execute after a slight delay if the provided group is not local this syntax does not return a reference to the created unit (see #Example 7)

Note

Armed Assault: Operation Flashpoint v1.34 Armed Assault v1.00 This command was sometimes bugged in Operation Flashpoint or Armed Assault; an additional join may solve the problem.

However, some commands such as setUnitPos only work if run before the join.

Syntax

Syntax 1

group createUnit [type, position, markers, placement, special]

Parameters:

Returns: Object - the created unit

Syntax 2

type createUnit [position, group, init, skill, rank]

Parameters:

Returns: > [!WARNING]

Nothing - this syntax does not return a unit reference! See #Example 7 for a workaround.

Examples

Example 1

_unit = group player createUnit ["B_RangeMaster_F", position player, [], 0, "NONE"];

Example 2

"B_RangeMaster_F" createUnit [position player, group player];

Example 3

"B_RangeMaster_F" createUnit [getMarkerPos "barracks", _groupAlpha];

Example 4

"B_RangeMaster_F" createUnit [
    getMarkerPos "marker_1",
    _groupAlpha,
    "loon1 = this; this addWeapon 'BAF_L85A2_RIS_SUSAT'",
    0.6,
    "corporal"
];

Example 5

_veh = "O_Quadbike_01_F" createVehicle (player getRelPos [10, 0]);
_grp = createVehicleCrew _veh;
_unit = _grp createUnit [typeOf driver _veh, _grp, [], 0, "CARGO"];

Example 6

Creating a unit from a different side may lead to issues:

_grp = createGroup east;
hint str side _grp; // EAST
_ap = _grp createUnit ["C_man_p_beggar_F", position player, [], 0, "NONE"];
hint str side _ap; // CIV, not EAST

// workaround
[_ap] joinSilent _grp;
hint str side _ap; // EAST

Example 7

Reference the created unit through a global variable:

_myUnit = "B_RangeMaster_F" createUnit [position player, group player];            // wrong - this syntax does not return a reference

"B_RangeMaster_F" createUnit [position player, group player, "myUnit = this"];    // correct - the unit is myUnit (NOT _myUnit!)

Multiplayer

It is recommended to create the unit where the group is local - use remoteExec if needed.

See also

createCenter · createGroup · createVehicle · setVehiclePosition · create3DENEntity

Community notes

Killzone_Kid

Alt Syntax is the older syntax and differs in functionality from the main, newer syntax. The main difference is that the older syntax does not return unit reference. This is because the unit created with Alt Syntax is created strictly where passed group is local. This means that if the group is remote the unit will be created on the different client than the one the command was executed on and therefore it is not possible to return created unit reference immediately. In contrast, the newer syntax allows creating units in remote groups while returning unit reference immediately, which could be unsafe and the appropriate warning is logged into .rpt file: Warning: Adding units to a remote group is not safe. Please use setGroupOwner to change the group owner first.

Another very important difference is that the older syntax (Alt Syntax) will create unit of the same side as the side of the group passed as argument. For example, if the group is WEST and the unit faction is OPFOR of type, say "O_Soldier_GL_F", the unit created will be on the WEST side as well. In contrast, newer syntax will create the same unit on the EAST side in the WEST group, which will be treated as hostile by other group members and eliminated.

Warning

Beware that in MP if unit is created in remote group with older syntax, the unit init will execute on calling client sometime in the future, after the unit is created on remote client, therefore the following code will fail:

// real example of the bad code
"O_Soldier_AR_F" createUnit [position player, someRemoteGroup, "thisUnit = this"];
publicVariable "thisUnit";
hint str isNil "thisUnit"; // true!
// the unit reference is nil because init statement has not been executed on this client yet
fusselwurm

Arma 3 v1.98 note that even when setting the placement special parameter to "NONE", 3DEN-placed objects are being ignored when looking for a free position. In other words: units will spawn within editor-placed rocks or under houses.