Nearest nth Object


You must be logged in to obtain assets


GameMaker Studio's "instance_nearest()" function can be useful, but have you ever wanted to find the 2nd or 3rd nearest object? -Or exclude the calling instance from the search? Now you can!

This script finds all instances of the object that is being searched for and returns the "nth" nearest instance of the object. For example, you could search for the 4th nearest monster object.

Format: scr_find_nearest_nth_object(x,y,obj,n,notme)

x,y - The x and y position to check from.

obj - The object to search for.

n - The nth object away from x, y.

notme - Exclude calling instance in the search. (0 = Include the calling instance, 1 = Exclude the calling instance. This applies only if the calling instance has the same object_index or a child of the object that is being seached for.)

The script returns the instance ID of the nth nearest object.

The included project shows how to easily use this script.


This asset also includes 'scr_find_farthest_nth_object()', which allows you to find the nth fartherst object in a similar way.

These scripts can be used on any platform.

End User Licence Agreement (EULA).

Age Rating: 4+


GMS1 - Version 1.0.0. Published December 18, 2015

Loading, please wait

Package contents

Loading, please wait

What is the issue?

Loading, please wait