Boolean flickers true and false - c#

In my game when the enemy detects the player from behind them the boolean facingRight should equal true and the enemy should flip 180 degrees on the x axis and run towards the player.

the enemy runs towards the player however facingRight flickers true and false which makes the enemy Flip constantly!!

if(seePlayer != null)
			{
				Debug.Log("worked");
				anim.SetFloat("Speed",Mathf.Abs(1));
				rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
				facingRight = true;
				if (facingRight)
				{
					Flip();
					facingRight = false;
				}
			}

Full Code:

using UnityEngine;
using System.Collections;

public class EnemyScript : MonoBehaviour {

	bool seenPlayer;
	RaycastHit2D seePlayer;

	public Rigidbody2D rb;


	public Vector2 newPos;
	public Vector2 negativeNewPos;

	float maxSpeed = 3f;
	Animator anim;

	GameObject player;
	public bool facingRight = false;

	public float sightDistance;



	void Start()
	{
		anim = GetComponent<Animator> ();
		rb = GetComponent<Rigidbody2D> ();
		player = GameObject.FindWithTag("Player");
	}

	void Update()
	{

	}

	void FixedUpdate()
	{
		//Just a debug visual representation of the Linecast, can only see this in scene view! Doesn't actually do anything!
		Debug.DrawRay(transform.position, newPos , Color.magenta);
		Debug.DrawRay(transform.position, negativeNewPos , Color.magenta);
		
		if(Physics2D.Raycast(transform.position,newPos,sightDistance, 1 << 8))
		{
			seePlayer = Physics2D.Raycast(transform.position,newPos, sightDistance, 1 << 8);
			seenPlayer = true;
			if(seePlayer != null)
			{
				Debug.Log("worked");
				anim.SetFloat("Speed",Mathf.Abs(1));
				rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
				facingRight = true;
				if (facingRight)
				{
					Flip();
					facingRight = false;
				}
			}
		}
		else if(Physics2D.Raycast(transform.position,negativeNewPos,sightDistance, 1<< 8))
		{
			seePlayer = Physics2D.Raycast(transform.position,negativeNewPos,sightDistance, 1 << 8);
			seenPlayer = true; //since the linecase is touching the player and we are in range, we can now interact!
			facingRight = false;
			if(seePlayer != null)
			{
				Debug.Log("worked");
				anim.SetFloat("Speed",Mathf.Abs(1));
				rigidbody2D.velocity = new Vector2(-maxSpeed, rigidbody2D.velocity.y);
				facingRight = false;
				if (facingRight)
				{
					Flip();
				}
			}
		}
		else
		{
			seenPlayer = false; //if the linecast is not touching a guard, we cannot interact
			anim.SetFloat ("Speed",0);
		}
	}
	void Flip ()
	{
		facingRight = !facingRight; // flips the character
		Vector3 theScale = transform.localScale; //flips the local scale
		theScale.x *= -1; //flip the x axis 
		transform.localScale = theScale; //apply all of this back to the local scale
	}
	void movingRight()
	{
		Vector2 moveRight = transform.localScale;
		moveRight.x *= -1;
		transform.localScale = moveRight;
	}
}

Hi

some parts of your code don’t make sense. You check for facingRight == true right after you set it to true.

You can use a flag that indicates if you have flipped already.

bool flipped = false; // define flipped-flag somewhere
...
...
if(seePlayer != null)
{
     Debug.Log("worked");
     anim.SetFloat("Speed",Mathf.Abs(1));
     rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
     // facingRight = true;
     if (!flipped)
     {
          Flip();
          facingRight = true;
          flipped = true;
     }
}

Not sure if it will work, probably depends on the rest of your code.

Edit, rethought:
Just dont change the flipped right variable and don’t check it.

if(seePlayer != null)
{
     Debug.Log("worked");
     anim.SetFloat("Speed",Mathf.Abs(1));
     rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
     facingRight = true;
     Flip();
}

Note
However if you have this codeblock in the Update() Function you need the first attempt I posted because otherwise it will still constantly flip.