How to freeze all enemy objects at once

I’ve looked around and I found a few different things but nothing that seemed to really work for what I was looking for specifically. I’m creating a Dodger game, basically things fall from the sky and you have to dodge. However, if you get hit, I want all of the enemies to freeze where they’re at until the scene ends. I don’t know how to get them to freeze.

I found this answer (lines 5-8 on best answer), but also found that FreezeAll doesn’t work with kinematic rigidbody2d, so I tried it slightly different but it doesn’t seem to work for me for some reason.

Here is my code so far. This code does freeze the singular enemy that hits the player, but not all enemies, which is what I’m going for. Thank you in advance.

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

public class Enemy : MonoBehaviour
{
    float currentSpeed;

    Player player;
    public float minEnemySpeed = 3f;
    public float maxEnemySpeed = 7f;

    bool playerDied = false;

    void Awake()
    {
        player = FindObjectOfType<Player>();
        currentSpeed = Random.Range(minEnemySpeed, maxEnemySpeed);
    }
    
    void Update()
    {
        if(!playerDied)
        {
            transform.Translate(Vector2.down * currentSpeed * Time.deltaTime);
        }
        else
        {
            Enemy[] enemies = Object.FindObjectsOfType<Enemy>();
         
            foreach (Enemy enemy in enemies)
            {
                transform.Translate(Vector2.zero);
            }
        }
    }

    void OnTriggerEnter2D(Collider2D collider)
    {
        player = collider.gameObject.GetComponent<Player>();
        if(!player) { return; }
        playerDied = true;
        Destroy(collider.gameObject);
        player.Die();
    }
}

Okay, so I believe what’s happening here is that when the player dies only the enemy that hits the player will have their player dies flag set to true, all the other enemies will still run their own update loop and therefore still translate downwards. By running the Translate(Vector2.Zero) on the other enemies, we’re not overwriting their own translate operations so this is redundant as unity_HTr7mOeTIRzvVA was saying.

You already have a reference to your player so you can just check against this instead of each enemy having their own variable. Your player script will / could have a function to determine whether it’s alive so replace !playerDied with a !player.IsDead().

Let me know if this works for you / if you have any questions :slight_smile: