{"name":"nearestObject","operator":null,"title":"nearestObject","type":"command","url":"https://community.bistudio.com/wiki/nearestObject","revid":377544,"updated":"2026-01-02T06:54:36Z","groups":["Object Detection"],"status":"ok","replaced_by":[],"games":[{"game":"ofp","name":"Operation Flashpoint","version":"1.00"},{"game":"ofpe","name":"Operation Flashpoint: Elite","version":"1.00"},{"game":"arma1","name":"Armed Assault","version":"1.00"},{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"ofp","name":"Operation Flashpoint","version":"1.00"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Returns the nearest object of given type (or inherited classes) to given position within a sphere. Hardcoded radius is 50 meters.\nUnlike with nearestObjects, where distance is measured in 2D space, nearestObject will be closest object in 3D space.\n\n> [!NOTE]\n> - in Operation Flashpoint, only objects of the exact provided class are found.\n> - if an object ID is used (#Syntax 4, see #Example 2) the search range is unlimited","syntaxes":[{"syntax":"nearestObject [position, type]","since":null,"effect":null,"exec":null,"params":[{"name":"position","type":"Object or Position2D or Position3D","optional":false,"default":null,"description":"position to start search at","types":["Object","Position2D","Position3D"],"since":null,"text":"position: Object or Position2D or Position3D - position to start search at"},{"name":"type","type":"String","optional":false,"default":null,"description":"type of object (see typeOf, class type)","types":["String"],"since":null,"text":"type: String - type of object (see typeOf, class type)"}],"returns":{"type":"Object","types":["Object"],"description":"nearest object, objNull otherwise","text":"Object - nearest object, objNull otherwise"},"form":"unary","operator":null,"left":null,"right":{"text":"[position, type]","types":["Array"]}},{"syntax":"nearestObject position","since":null,"effect":null,"exec":null,"params":[{"name":"position","type":"Position2D or Position3D","optional":false,"default":null,"description":"position to start search at","types":["Position2D","Position3D"],"since":null,"text":"position: Position2D or Position3D - position to start search at"}],"returns":{"type":"Object","types":["Object"],"description":"nearest object, objNull otherwise","text":"Object - nearest object, objNull otherwise"},"form":"unary","operator":null,"left":null,"right":{"text":"position","types":["Position2D","Position3D"]}},{"syntax":"position nearestObject typeOrId","since":null,"effect":null,"exec":null,"params":[{"name":"position","type":"Position3D or Position2D","optional":false,"default":null,"description":"position to start search at","types":["Position3D","Position2D"],"since":null,"text":"position: Position3D or Position2D - position to start search at"},{"name":"typeOrId","type":"String","optional":false,"default":null,"description":"type of object (see typeOf, class type) or Arma 3 v2.10 Visitor id (see getObjectID)","types":["String"],"since":null,"text":"typeOrId: String - type of object (see typeOf, class type) or Arma 3 v2.10 Visitor id (see getObjectID)"}],"returns":{"type":"Object","types":["Object"],"description":"nearest object, objNull otherwise","text":"Object - nearest object, objNull otherwise"},"form":"binary","operator":null,"left":{"text":"position","types":["Position3D","Position2D"]},"right":{"text":"typeOrId","types":["String"]}},{"syntax":"position nearestObject id","since":null,"effect":null,"exec":null,"params":[{"name":"position","type":"Position2D or Position3D","optional":false,"default":null,"description":"position to start search at","types":["Position2D","Position3D"],"since":null,"text":"position: Position2D or Position3D - position to start search at"},{"name":"id","type":"Number","optional":false,"default":null,"description":"object Visitor id","types":["Number"],"since":null,"text":"id: Number - object Visitor id"}],"returns":{"type":"Object","types":["Object"],"description":"nearest object, objNull otherwise","text":"Object - nearest object, objNull otherwise"},"form":"binary","operator":null,"left":{"text":"position","types":["Position2D","Position3D"]},"right":{"text":"id","types":["Number"]}}],"examples":[{"text":"```sqf\n_nObject = nearestObject [2345, 6789];\n_nObject = nearestObject [player, \"StreetLamp\"];\n```","code":["_nObject = nearestObject [2345, 6789];\n_nObject = nearestObject [player, \"StreetLamp\"];"]},{"text":"Return the object with ID 123456:\n\n```sqf\n_nObject = [0,0,0] nearestObject 123456;\n```","code":["_nObject = [0,0,0] nearestObject 123456;"]},{"text":"```sqf\n_nObject = getPos player nearestObject \"StreetLamp\";\n```","code":["_nObject = getPos player nearestObject \"StreetLamp\";"]},{"text":"Return the nearest object with (typeOf _nObject == \"#XXXX\") -> #mark, #slop, etc. Unlimited search range:\n\n```sqf\n_nObject = _position nearestObject -1;\n```\n\nReturn the nearest object with (typeOf _nObject != \"\"). Search range is 50m:\n\n```sqf\n_nObject = nearestObject _position;\n```","code":["_nObject = _position nearestObject -1;","_nObject = nearestObject _position;"]},{"text":"(See also allMissionObjects):\n\n```sqf\n_blood    = nearestObject [player, \"#slop\"];\n_step    = nearestObject [player, \"#mark\"];\n_track    = nearestObject [player, \"#track\"];\n_crater    = nearestObject [player, \"#crater\"];\n```","code":["_blood\t= nearestObject [player, \"#slop\"];\n_step\t= nearestObject [player, \"#mark\"];\n_track\t= nearestObject [player, \"#track\"];\n_crater\t= nearestObject [player, \"#crater\"];"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["nearestObjects","nearObjectsReady","nearObjects","objectFromNetId","nearEntities","entities","object","nearestBuilding","nearestTerrainObjects","nearestMines"],"notes":[{"user":"Gnashes","date":"2019-01-29T15:31:00Z","text":"The ID for Syntax 4 may be dynamically obtained via the following (as of Arma 3 v1.88):\n(for Arma 3 v2.10 or newer, use getObjectID instead)\n\n```sqf\n// returns Object Terrain/Visitor ID, or -1 if no ID found (invalid/null object)\ncursorObject call {\n    private _obj = str (param [0, objNull]);\n    private _id = -1;\n\n    private _find = ((_obj find \"#\") + 2);\n    if (_find > 1) then\n    {\n        private _len = ((_obj find \":\") - _find);\n        _id = _obj select [_find, _len];\n        parseNumber _id;\n    };\n    _id\n};\n```"},{"user":"blackfisch","date":"2020-02-12T08:55:00Z","text":"Note, that if you want to check for the nearest Unit you have to check for `CAManBase` instead of `Man`, since `Man` will also return any wildlife population in that area:\n\n```sqf\nnearestObject [player, \"Man\"];            // could possibly return rabbits or other wildlife\nnearestObject [player, \"CAManBase\"];    // only returns units\n```"}],"categories":["ArmA: Armed Assault: Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Object Detection","Introduced with Operation Flashpoint version 1.00","Operation Flashpoint: Elite: Scripting Commands","Operation Flashpoint: New Scripting Commands","Operation Flashpoint: Scripting Commands","Scripting Commands","Take On Helicopters: Scripting Commands"]}