Changing 2 objects position over time

I have the following code which I call in my Update() function:

void changeCapsulePosition()
	{
		int timer;
		timer = (int)Time.time;

		if(GUICounters.score >= 1)
		{
			Debug.Log(timer);
			if(timer % 5 == 0 && hasChanged == false)
			{

				Vector3 tempPosition = capsule1.transform.position;
				capsule1.transform.position = capsule2.transform.position;
				capsule2.transform.position = tempPosition;
				hasChanged = true;

			}
            hasChanged = false;
		}

	}

I would like my 2 objects to change position once after every 5 seconds but as it is, it switches back and forth the entire time the timer is on 5. I’m assuming this is because it is being called by Update and is ran quite a few times and since I change the hasChanged variable back to false, the condition will be true throughout the entire second.

Is there a way I can just get it to run once every 5 seconds?

Cheers @robertbu I set up an InvokeRepeating function which makes it behave accordingly. Seems so simple when its laid out now. I set it up to choose a random time between 1 and 5.

Thanks again, new code for anyone wondering:

    void Start () 
	{
		InvokeRepeating("changeCapsulePosition", 2, Random.Range (0,5));
	}

	// Update is called once per frame
	void Update () 
	{

	}

	
    void changeCapsulePosition()
	{
		if(GUICounters.score >= 1)
		{
			Vector3 tempPosition = capsule1.transform.position;
			capsule1.transform.position = capsule2.transform.position;
			capsule2.transform.position = tempPosition;
		}

	}