How do i change permanently a boolean variable in a OnCollisionEnter2D Function?

Hi, i’m trying to make a simple script for a Space Invaders clone in Unity, i have this script where after the Player gets hitted by the enemy bullet he dies and the player get destroyed, after trhee seconds a new player will appear and loose a life. I got stuck because the variable IsAlive changes to false only on the frame where the Player gets hitted and then return to true, skipping all the code.

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

public class PlayerLife : MonoBehaviour
{
    public GameObject Player;
    public int PlayerLifes = 3;
    public float RespawnCooldown;
    public Transform RespawnPoint;
    public float time;
    public int MaxLife = 3;
    bool IsAlive=true;
    void Start()
    {
        Player.GetComponent<GameObject>();

    }
    
    void Update()
    {

        if (!IsAlive) { 
            Destroy(Player);
            time += Time.deltaTime;
            LoseLifes(time,RespawnCooldown);
        } 

    }

    public void LoseLifes(float timer, float cooldown)
    {
        bool loselife = false;
        Debug.Log(timer);
        if (timer > cooldown&&!loselife)
        {
            GameObject.Instantiate(Player, RespawnPoint.position, transform.rotation);
            loselife = true;
            IsAlive = true;
        }

        if (PlayerLifes == 0 && !loselife)
        {
            GameOver();
        }

        if (loselife) {
            PlayerLifes = PlayerLifes-1;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "EnemyBullet")
        {
            PlayerKilled();
        }
    }


    void PlayerKilled()
    {
        IsAlive=false;
    }
    void GameOver()
    {
        Debug.Log("Game over");
    }


}

I can’t say for sure what else may be going on, but I would simplify the code you provided as follows:

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

public class PlayerLife : MonoBehaviour
{
    public GameObject PlayerInstance;
    public int PlayerLifes = 3;
    public float RespawnCooldown;
    public Transform RespawnPoint;
    public float time;
    public int MaxLife = 3;
	float PlayerKilled = 0;
	
    void Start()
    {

    }
    
    void Update()
    {
		if (PlayerKilled > 0 && (Time.realtimeSinceStartup - PlayerKilled) >= RespawnCooldown)
		{
			PlayerLifes--;
			PlayerKilled = 0;
			if (PlayerLifes > 0)
			{
				// re-enable the player instance, make sure you call any necessary code in the scope of OnEnable to re-initialize the position and any weapons the player object may contain 
				PlayerInstance.SetActive(true); 	
			}
			else
			{
				GameOver();
			}
		}
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "EnemyBullet")
        {
			// disable, do not destroy, this will allow the object to remain in memory instead of reallocating from the heap, which tends to cause hitches
			PlayerInstance.SetActive(false);
			PlayerKilled = Time.realtimeSinceStartup;
        }
    }

    void GameOver()
    {
        Debug.Log("Game over");
    }


}

There is place in your code where you are setting IsAlive to true. Check and make sure that’s not running the next frame. You’re code is probably just IsAlive to true again right after you set it to false.