respawn delay problem

I have this health script that I have somewhat set up to have a delay before respawning, Anyway for Some reason due to the Update function when the player dies it continuously spawns the death effect, point cube and continuously disables all the assigned components so It makes the character unplayable after dying once. Is there a way to change the update function to still do the same action after the player dies but allows for the delay and everything else to work ?

here’s the script:

 var timer : float = 0.0; // begins at this value
 var timerMax : float = 3.0;
var PointCube : Transform;
 var spawneffect : Transform;
 var deatheffect : Transform;
 var deathcam : Transform;
 var playercollider : Collider;
 var PlayerDamageTrigger : Collider;
 public var player1move : Player1Walk;
 public var player1aim : Player1Aim;
 var GunHolder : GameObject;
 var playerPhysics : Rigidbody;
 var PlayerCam : GameObject;
  var SpawnPoints: Transform[];
  var respawn : boolean = false;
 var health:float = 90;
 var healthText : UI.Text;

 function Start(){
 playerPhysics = GetComponent.<Rigidbody>();
 }

  function Damage(dmg:float){
  health -= dmg;
  }
  
  function FixedUpdate (){
  if(healthText)healthText.text = health.ToString();
  if(health ==0){
    Instantiate (deatheffect,transform.position,transform.rotation);
    Instantiate (PointCube,transform.position,transform.rotation);
    Instantiate (deathcam,transform.position,transform.rotation);
    player1move.enabled = false;
    player1aim.enabled = false;    //disable player turning script
    playercollider.enabled = false;
    PlayerDamageTrigger.enabled = false;
  playerPhysics.isKinematic = true;
    playerPhysics.detectCollisions = false;
 PlayerCam.gameObject.active = false;
 GunHolder.gameObject.active = false;
 
      Respawn();
  }
  }
 
  function Respawn(){
   timer += Time.deltaTime;
      
     
     if (timer >= timerMax){
 
 
         var spawnPointID: float = Random.Range(0, SpawnPoints.length);
 
      transform.position = SpawnPoints[spawnPointID].position;
 
     Instantiate (spawneffect,transform.position,transform.rotation);
 
   var audio: AudioSource = GetComponent.<AudioSource>();
     audio.Play();
     audio.Play(44100);
    player1move.enabled = true;
    player1aim.enabled = true; 
     playercollider.enabled = true;
    PlayerDamageTrigger.enabled = true;
    playerPhysics.isKinematic = false;
    playerPhysics.detectCollisions = true;
    PlayerCam.gameObject.active = true;
 GunHolder.gameObject.active = true;
 
   health = 100;
  }
  }

it also says that the Update function cant be a coroutine

@EliteHedgehog56

I think this should work:

    var timer : float = 0.0; // begins at this value
    var timerMax : float = 3.0;
    var PointCube : Transform;
    var spawneffect : Transform;
    var deatheffect : Transform;
    var deathcam : Transform;
    var playercollider : Collider;
    var PlayerDamageTrigger : Collider;
    public var player1move : Player1Walk;
    public var player1aim : Player1Aim;
    var GunHolder : GameObject;
    var playerPhysics : Rigidbody;
    var PlayerCam : GameObject;
    var SpawnPoints: Transform[];
    var respawn : boolean = false;
    var health:float = 90;
    var healthText : UI.Text;
 
    function Start()
    {
        playerPhysics = GetComponent.< Rigidbody > ();
    }

    function Damage(dmg:float)
    {
        health -= dmg;
    }

    function FixedUpdate()
    {
        if (healthText)
            healthText.text = health.ToString();
        if (health == 0)
        {
            Instantiate(deatheffect, transform.position, transform.rotation);
            Instantiate(PointCube, transform.position, transform.rotation);
            Instantiate(deathcam, transform.position, transform.rotation);
            player1move.enabled = false;
            player1aim.enabled = false;    //disable player turning script
            playercollider.enabled = false;
            PlayerDamageTrigger.enabled = false;

            playerPhysics.isKinematic = true;
            playerPhysics.detectCollisions = false;
            PlayerCam.gameObject.active = false;
            GunHolder.gameObject.active = false;

            Respawn();
        }
    }

    function Respawn()
    {
        timer += Time.deltaTime;


        if (timer >= timerMax)
        {
            var spawnPointID: float = Random.Range(0, SpawnPoints.length);

            transform.position = SpawnPoints[spawnPointID].position;

            Instantiate(spawneffect, transform.position, transform.rotation);

            var audio: AudioSource = GetComponent.< AudioSource > ();

            //audio.Play();
            //audio.Play(44100);
            audio.PlayOneShot(audioClip, 1.0f);
            player1move.enabled = true;
            player1aim.enabled = true;
            playercollider.enabled = true;
            PlayerDamageTrigger.enabled = true;
            playerPhysics.isKinematic = false;
            playerPhysics.detectCollisions = true;
            PlayerCam.gameObject.active = true;
            GunHolder.gameObject.active = true;

            health = 100;
        }
    }

The issue is that the update functions ( Update(), FixedUpdate(), and LateUpdate()) are called repeatedly, so the death effect will be instantiated as long timer in respawn doesn’t respawn the player.

Instead of checking only if (health == 0), you should either also check if player death was detected (for example by checking if the player is respawning or not), or even better, check in Damage() if health is 0 (actually, you should check <= 0). So move the whole if (health == 0) block from FixedUpdate() to Damage(), and it should work.

As an aside FixedUpdate() is called every time the physics is updated (by default, every 0.02 seconds), that is, it is NOT called on every frame. In general, you should only use FixedUpdate() for physics related logic. For game logic and user input usually Update() is used.