I need a line of code to stop player being destroyed when shield is on!

Hello there, I have lil problem Im still learning coding C# and I can’t figure out how to stop my enemy from killing the player when it enters on Collision with it when the Player has the shield on, any thoughs on that I 'll really apreciate it.

here’s my Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private Rigidbody theRB;
public ParticleSystem explosionParticle;
public bool hasShield = false;
public bool gameOver;
private Animator animator;
public GameObject ShieldIndicator;

// Start is called before the first frame update
void Start()
{
    theRB = GetComponent<Rigidbody>();
    animator = GetComponent<Animator>();

}

// Update is called once per frame
void Update()
{
    theRB.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, theRB.velocity.y, Input.GetAxis("Vertical") * moveSpeed);

   
}

private void OnTriggerEnter(Collider collider)
{

    if (collider.CompareTag ("PowerPill"))
    {

        hasShield = true;
        ShieldIndicator.gameObject.SetActive(true);
        gameObject.GetComponent<Animator>().Play("Xplosion");
        StartCoroutine(ShieldCountDownRoutine());

    }

}



private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.CompareTag("Enemy") && hasShield)
    {
        Rigidbody enemyRigidbody = collision.gameObject.GetComponent<Rigidbody>();
        
    }

    

    if (collision.gameObject.CompareTag("Enemy"))
    {

        

        Debug.Log("Game Over");
        gameOver = true;
        Destroy(gameObject);
        
    }

}

IEnumerator ShieldCountDownRoutine()
{

    yield return new WaitForSeconds(5);
    hasShield = false;
    ShieldIndicator.gameObject.SetActive(false);
}

}

Can’t you just use your hasShield flag? i.e in OnCollisionEnter change

if (collision.gameObject.CompareTag("Enemy"))

to

if (collision.gameObject.CompareTag("Enemy") && !hasShield)

You use that flag just before that line, but only to decide whether or not to define enemyRigidbody (which you don’t then use, making that whole section superfluous).

Sorry but my late reply I used and it worked, Thanks a lot Bonfire-Boy!!