{"name":"nearestObject","title":"nearestObject","type":"command","url":"https://community.bistudio.com/wiki/nearestObject","revid":377544,"updated":"2026-01-02T06:54:36Z","groups":["Object Detection"],"status":"ok","games":{"ofp":"1.00","ofpe":"1.00","arma1":"1.00","arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"ofp 1.00","arma3_since":"0.50","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]","params":[{"name":"position","type":"Object or Position2D or Position3D","description":"position to start search at","type_expr":"Object | Position2D | Position3D"},{"name":"type","type":"String","description":"type of object (see typeOf, class type)","type_expr":"String"}],"returns":{"type":"Object","type_expr":"Object","description":"nearest object, objNull otherwise"},"form":"unary","right":{"text":"[position, type]"}},{"syntax":"nearestObject position","params":[{"name":"position","type":"Position2D or Position3D","description":"position to start search at","type_expr":"Position2D | Position3D"}],"returns":{"type":"Object","type_expr":"Object","description":"nearest object, objNull otherwise"},"form":"unary","right":{"text":"position"}},{"syntax":"position nearestObject typeOrId","params":[{"name":"position","type":"Position3D or Position2D","description":"position to start search at","type_expr":"Position3D | Position2D"},{"name":"typeOrId","type":"String","description":"type of object (see typeOf, class type) or Arma 3 v2.10 Visitor id (see getObjectID)","type_expr":"String"}],"returns":{"type":"Object","type_expr":"Object","description":"nearest object, objNull otherwise"},"form":"binary","left":{"text":"position"},"right":{"text":"typeOrId"}},{"syntax":"position nearestObject id","params":[{"name":"position","type":"Position2D or Position3D","description":"position to start search at","type_expr":"Position2D | Position3D"},{"name":"id","type":"Number","description":"object Visitor id","type_expr":"Number"}],"returns":{"type":"Object","type_expr":"Object","description":"nearest object, objNull otherwise"},"form":"binary","left":{"text":"position"},"right":{"text":"id"}}],"examples":[{"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;"]},{"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\"];"]}],"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```"}]}