if statement constantly triggering

I try to figure everything out on my own without bothering other people but this has beaten me. I have been stuck on this problem for a while and have tried everything I can think of.

Here is the problem section of code.

using UnityEngine;
using System.Collections;

public class AutoAttackMelee : MonoBehaviour {
	
	public bool attackOn = false;
		
		
	
	// Update is called once per frame
	void Update () {
		
		if (attackOn = true) {
		    attack ();
		}
	}
	
	
	
	private void attack(){

      Debug.Log ("attackOn");

		/*	PlayerStats aam = (PlayerStats)GetComponent ("PlayerStats");
		aam.adjustcurHealth (-10);
	}

*/
	}

}

The problem is no matter what I do the “if(attackOn = true) attack();” constantly spams. I know that the function of attack in the bottom has been disabled. It actually worked and would drop the health bar instantly to 0. Also note there should be no way for attackOn to be set to true in this script. I have deleted the script PlayerStats and all other scripts from my project so they cannot be triggering attackOn to true.

Things I have tried:

-changing the = to ==

-Changing the = true to =!false (a long shot I know)

  • changing attackOn from bool to int setting it’s value to 1 and triggering the “if” on a value of 0

-adding and removing various {} around the if statement

-changing the name of the attackOn

-writing a similar script with different names

  • deleted my entire project folder and restored from a back up a few days ago (long before I had this problem)

Help me Obi Wan Codenobi you’re my only hope

Do you have the:

public bool attackOn = false;

Set to ‘true’ in the inspector on your GameObject? The value assigned in the inspector overrides the value assigned in your public declaration in code.
If you want to set it to false initially, either make it private, use HideInInspector, or assign the declaration in a Start or Awake function.

Also as a side note, it should read:

void Update () 
{
     if( attackOn ) 
     {
           attack();
     }
}