Enemy movement after death

Hello guys
I’m new to unity and coding in general , and trying to make my first game

So I managed to make the enemy show “death animation” after jumping on its head, but it still moves untill it’s destroyed

Tried getting to the scrips of the enemy from the player’s, but I get a console error- object reference not set to an instance of an object (line with EnemyMove.speed)

that’s the player’s script (cut it short for ya, tell me if I need to add the full script)

public class PlayerMovement : MonoBehaviour
{

//kill enemies
public GameObject jumpkill;
private Animator DieAnm;
private EnemyMove EnemyMove;

// Start is called before the first frame update
void Start()
{
  
    //enemy
    EnemyMove =  GetComponent<EnemyMove>();   
    DieAnm = jumpkill.GetComponent<Animator>();

}

private void OnCollisionEnter2D(Collision2D collision)
{    
    if (collision.gameObject.tag == "Stomp")
    {
        Destroy(jumpkill, 1);
        DieAnm.SetTrigger("Die");
        EnemyMove.speed = 0;

    }
}

}

that’s the enemy script

public class EnemyMove : MonoBehaviour {

public float speed;
public Vector2 movement;


public Animator DieAnm;
// Start is called before the first frame update
void Start()
{
    speed = 1 * Time.deltaTime;
    movement = new Vector2(speed, 0);
  
}

// Update is called once per frame
void Update()
{
    transform.position = new Vector2(transform.position.x + movement.x, transform.position.y);
    if (transform.position.x >= 41)
    {
        movement = new Vector2(-speed, 0);
        transform.localScale = new Vector2(-0.45f, 0.45f);
    }

    if (transform.position.x <= 34)
    {
        movement = new Vector2(speed, 0);
        transform.localScale = new Vector2(0.45f, 0.45f);
    }
}  

}

@yakirh95
I don’t quite understand where the enemymove Script is attached to and what jumpkill is, but I can probably explain the Error Message:

         Destroy(jumpkill, 1);
         DieAnm.SetTrigger("Die");
         EnemyMove.speed = 0;

If I understood correctly, EnemyMove is attached to jumpkill?!. If thats the case, you’re destroying the GameObject and then trying to access the script, which was attached to jumpkill. But its gone and the reference you did with EnemyMove = GetComponent<EnemyMove>(); doesnt exist anymore. Thats why it say Reference not set to an Instance… . Just try swapping the speed = 0 and the Destroy() Line.

Try changing EnemyMove = GetComponent<EnemyMove>(); to EnemyMove = GameObject.FindObjectOfType<EnemyMove>();

You are trying to access the enemy speed after marking it for destruction, Unity won’t allow that so it throws an exception.

Btw, handling the logic for killing enemies directly in the player is a pretty bad idea. The enemy itself should handle all that.

So instead of the player getting references to the enemy and handling all the logic for killing the enemy it should simply call a function inside the enemy class that handles everything.

The way you have it setup right now is very very bad practice and will bite you later.

There is no reason for the player to know how or why the enemy works the way it works, it should just check for the collision then tell the enemy to die.