# Unity 5 - Place objects in game to closest gameobject

I am making a puzzle game in unity3D 5 for school and one of the puzzles is this: The player (third person) can pick up certain objects (4 objects) and has to place them inside holes in a wall in a specific order for a door to open.

I have 4 empty gameobjects named as slot001, slot002, slot003 and slot004 at the right positions at the holes in the wall. When the player has an item held and a certain button is pressed I link the transform of the object to the transform of the empty gameobject.

The problem is that the objects transform has to link to the transform of the closest gameobject. So when you stand in front of the slot002 gameobject the objects transform should link to the transform of slot002. The same goes for slot 001, 003 and 004 ofcourse. I am not sure how to do this.

I put the 4 empty gameobjects (slots) in a list. I know of the Vector3.Distance() function and I tried using that to check for the closest distance with Mathf.Min() but I couldn't quite get it to work.

Any help or a nudge in the right direction would be appreciated!

Thanks.

GameObject closestObject = null;
var distance = SnapDistance; // guarantees it will only snap once close enough.  Assuming some value that is inspector editable, like 3.
// if you don't want a snap distance, then set the starting distance to float.MaxValue
foreach(var slot in ListOfSlots) // ListOfSlots is GameObject[] or List<GameObject>
{
// gets the current distance from this object to the slot.
var currentSlotDistance = Vector3.distance(slot.transform.position, transform.position);

// check the distance.  if this is closer than the previous distance, then this is our closest object.  By checking each object to see if it is closer than the last, we will eventually find the closest one.
if (currentSlotDistance < distance)
{
// once found, set the distance to our current distance, and set the closestObject to the slot we are looking at.
distance = currentSlotDistance;
closestObject = slot;
}
}

if (closestObject != null) // we have a winner  closest under X distance
{
// assuming you wanted to move this object into the slot:
transform.position = closestObject.transform.position;
}

• Thanks for your response first of all! I think I get where you're going with this. Could you maybe convert it to c# though? I have never learnt javascript :) – S. Neut Mar 16 '17 at 12:34
• This looks like C# to me already. Did I miss an edit? If not, S. Neut, can you please clarify which parts might be giving you trouble? – DMGregory Mar 16 '17 at 16:24
• Hmm I'm not sure where in my code to place this piece of code. I forgot to link my code anyway, that was not handy of me :) Here is the code that I have now: pastebin.com/zs1S8Prq – S. Neut Mar 16 '17 at 17:05
• @S.Neut It'll probably go somewhere near //Place the object in the right slot. But this isn't a copy/paste solution. Your job as a developer is to take the algorithm presented above and implement it into your code. – MichaelHouse Mar 16 '17 at 21:59
• @Byte56 You are right, I will try my best to make it work and let you know if I did it or not. – S. Neut Mar 16 '17 at 22:58

So I almost got it to work thanks to the answer of Dan Violet Sagmiller, there is just one small problem left. I need to check if a slot is already occupied. How could I do this? I was thinking of an array of booleans but that didn't quite work.

Here is the current code that I have now: http://pastebin.com/XYQ4hBEX

• You can add the object as a child of the slot. And when determining which slot is closest you rule out those with children. Helpful hint: the children of an object can be found (oddly enough) under transform.children – lilotop Mar 18 '17 at 19:30