Auto-snap script?

I have some Gameobjects in my scene that needs to be snapped to the grid, i can hold Ctrl while moving them to snap them but is there a way to write a script for these Gameobjects that will auto-snap them when moved?

Thanks!

Well here’s what i came up with, i’m pretty new to coding so there might be a simpler solution, but it works

using UnityEngine;

[ExecuteInEditMode]
public class AutoSnap : MonoBehaviour 
{

    public float snapValueX;
    public float snapValueY;
    public float snapValueZ;


    void Update ()  
    {
        if (snapValueX != 0)
            transform.position = new Vector3(Mathf.Round(transform.position.x * (1 / snapValueX)) / (1 / snapValueX),transform.position.y, transform.position.z);

        if (snapValueY != 0)
            transform.position = new Vector3(transform.position.x, Mathf.Round(transform.position.y * (1 / snapValueY)) / (1 / snapValueY), transform.position.z);

        if(snapValueZ != 0)
            transform.position = new Vector3(transform.position.x, transform.position.y, Mathf.Round(transform.position.z * (1 / snapValueZ)) / (1 / snapValueZ)); 
    }  
}