How do I apply an ExplosionForce to certain gameobjects in an array?

The code I have right now is supposed to get the colliders that are in the Collider, then if it is a specific gameobject (which will be instantiated in) and the magnet toggle is set to true, then it will apply a reverse explosionforce.

However, the toggle doesn’t work and just remains on, and the magnet affects everything. Any help appreciated, I am really new.

using UnityEngine;
using System.Collections;

public class TractorFieldScript : MonoBehaviour {
	
	public float magnetStrength;
	
	private Transform Asteroid;
	private float distance;
	private bool magnetOn = false;
	
	void Start () {
	
	}
	
	void Update () {
		
		if(Input.GetKey("x") && (magnetOn = false)){
			magnetOn = true;
		}
		
		if(Input.GetKey("x") && (magnetOn = true)){
			magnetOn = false;
		}
		
		Collider[] colliders = Physics.OverlapSphere(rigidbody.position,5);
		foreach (Collider hit in colliders){
			
			if(!hit){
			continue;
			}
				
			if(hit.rigidbody && (magnetOn == true)){
					hit.rigidbody.AddExplosionForce(-magnetStrength, rigidbody.position, 5);
			}
			
		}
	
	}
}

You put ‘=’ instead of ‘==’. Change

   if(Input.GetKey("x") && (magnetOn = false)){
     magnetOn = true;
   }

   if(Input.GetKey("x") && (magnetOn = true)){
     magnetOn = false;
   }

to

   if(Input.GetKey("x") && (magnetOn == false)){
     magnetOn = true;
   }

   if(Input.GetKey("x") && (magnetOn == true)){
     magnetOn = false;
   }

but I suggest:

   if(Input.GetKey("x")){
     magnetOn = !magnetOn;
   }