Creating Local Gravity To Attract Specific GameObjects

Hi, I’m having little trouble with this. I want to create a magnet effect to draw my Coins gameObject towards the player.

using UnityEngine;
using System.Collections;
 
public class MagnetScript : MonoBehaviour {
 
    public GameObject attractedTo;
    public float strengthOfAttraction = 5.0f;
 
 
    void Update()
    {
       Vector3 direction = attractedTo.transform.position - transform.position;
       rigidbody2D.AddForce(strengthOfAttraction * direction);
 
    }
}

This is my code, but the problem is that all the other gameObjects are moving in the direction. Please help me with this

I think the magnitude would be wrong. A magnet is stronger the closer you are to it, but your code makes it stronger the further away it is.

Magnets are supposed to be proportional to the inverse of the square of the distance, so my first thought is to calculate the strength as strengthOfAttraction/direction.sqrMagnitude, then multiply that by the Normalize() of the direction vector.