Invokes & Coroutines not being called properly?

Invoke method not being called for unknown reason, used a Coroutine as well - no difference.
The function gets performed if I just straight out call it ( Example : FreezeZombies(); ) but if its either a Coroutine or an Invoke, which I need because I need a short delay, It just never gets called?

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

public class TeslaProjectile : Projectile
{
    [Header("Tesla")]
    [SerializeField] private ParticleSystem teslaInfectedParticles = null;
    [SerializeField] private float zombieFrozenTime = .2f;

    private List<UndeadHealth> undeadHealthsToShock = new List<UndeadHealth>();

    protected override void GrabSplashDamageEntitys(Collider[] splashHits)
    {
        base.GrabSplashDamageEntitys(splashHits);

        foreach (Collider c in splashHits)
        {
            if (c.TryGetComponent<PoweredObject>(out PoweredObject poweredObj))
            {
                if (poweredObj.poweredByElectricBursts)
                {
                    poweredObj.TurnPowerOn();
                }
            }

            if (c.TryGetComponent<UndeadLimb>(out UndeadLimb hitLimb) && hitLimb.GetComponentInParent<UndeadHealth>())
            {
                UndeadHealth _health = hitLimb.GetComponentInParent<UndeadHealth>();
                if (!_health.isInDeathState && !undeadHealthsToShock.Contains(_health))
                {
                    undeadHealthsToShock.Add(_health);
                    if (_health.TryGetComponent<Zombie>(out Zombie zmb))
                    {
                        zmb.isAllowedToDealDamage = false;
                        worldFunctionManager.SpawnParticle(teslaInfectedParticles, _health.thisEntity.GetEntityPosition(), true);
                    }
                }
            }
        }

        Invoke(nameof(FreezeZombies), zombieFrozenTime);
    }

    private void FreezeZombies()
    {
        if(undeadHealthsToShock.Count <= 0) return;

        foreach(UndeadHealth health in undeadHealthsToShock)
        {
            health.TakeDamage(damage, ef, health.thisEntity.GetEntityPosition());
            if (health.TryGetComponent<Zombie>(out Zombie zmb))
            {
                zmb.isAllowedToDealDamage = true;
                worldFunctionManager.SpawnParticle(teslaInfectedParticles, health.thisEntity.GetEntityPosition(), true);
            }
        }
    }
}

The most likely explanation is that the script is destroyed before the method can be called?

Invoke and coroutines are tied to the script they’re called on and if the script is destroyed they will no longer get called. You’d need to start the invoke/coroutine on another script that you know will live long enough or destroy the current script only after the method has been called.

Also note that Invoke and coroutines aren’t completely equivalent, Invoke will still be called if the script’s game object is deactivated but coroutines will be stopped (they both continue if only the script is disabled).

2 Likes

Yeah that was it, Cheers

1 Like