I've got a game that involves voxel-like game object management. Each voxel object snaps to the integer coordinates in Unity's scene, at play time—easy enough to do in the Start function. Just floor every coordinate, and your origin is where you need it to be.

However, I may be placing some of these voxels before runtime, and would very much like them to snap to the grid while I'm dragging them into the scene. (I don't have any specific advantage in placing such a mesh halfway between valid locations.) If there's a way to ensure this in the editor, great; if there's a way for me to enforce it in code, that's even better.

Does such a facility exist in Unity?

  • \$\begingroup\$ Have you tried using OnValidate? \$\endgroup\$
    – DMGregory
    Mar 22, 2022 at 16:02
  • \$\begingroup\$ I have not. Does that get called when the object is dropped in the scene? \$\endgroup\$ Mar 22, 2022 at 16:32
  • \$\begingroup\$ That seems like something you could test in about as much time as it takes to type that comment. I would, but I'm on a bus right now. \$\endgroup\$
    – DMGregory
    Mar 22, 2022 at 16:34
  • \$\begingroup\$ I appreciate the consideration... the project was loading until a moment ago, it's at a half gig in size! Anyway, this seems to do the job on compilation--everything snaps into place, but not on a change of the position value. I think it might be the right track though. \$\endgroup\$ Mar 22, 2022 at 16:43
  • \$\begingroup\$ What's also interesting is that it's called after Control+Z. \$\endgroup\$ Mar 22, 2022 at 16:45


You must log in to answer this question.

Browse other questions tagged .