The object of type "Transform" has been destroyed...

After my enemy reaches 0hp (I kill him) the game freezes and I get and error MissingReferenceException: The object of type ‘Transform’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.set_localScale (UnityEngine.Vector3 value) (at <480508088aee40cab70818ff164a29d5>:0)
EnemyAI.FixedUpdate () (at Assets/Scripts/EnemyAI.cs:82).
Here is my script. Btw I am very new to Unity and scripting :slight_smile: I followed Brackeys tutorial.

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

public class EnemyAI : MonoBehaviour
{

public Transform target;

public float speed = 200f;
public float nextWaypointDistance = 3f;

public Transform EnemyGFX;

Path path;
int currentWaypoint = 0;
bool reachedEndOfPath = false;

Seeker seeker;
Rigidbody2D rb;

// Start is called before the first frame update
void Start()
{
    seeker = GetComponent<Seeker>();
    rb = GetComponent<Rigidbody2D>();

    InvokeRepeating("UpdatePath", 0f, .5f);
    
}
void UpdatePath()
{
    if (seeker.IsDone())
        seeker.StartPath(rb.position, target.position, OnPathComplete);
} 
void OnPathComplete(Path p)
{
    if (!p.error)
	{
        path = p;
        currentWaypoint = 0;
	}
}
// Fixed used in physics etc.
void FixedUpdate()
{
    if (path == null)
        return;

    if(currentWaypoint >= path.vectorPath.Count)
	{
        reachedEndOfPath = true;
        return;
	}else
	{
        reachedEndOfPath = false;
	}

    Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
    Vector2 force = direction * speed * Time.deltaTime;

    rb.AddForce(force);

    float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);

    if (distance < nextWaypointDistance)
	{
        currentWaypoint++;
	}

    if (force.x >= 0.01f)
    {
        EnemyGFX.localScale = new Vector3(-8f, 8f, 1f);
    }
    else if (force.x <= -0.01f)
    {
        EnemyGFX.localScale = new Vector3(8f, 8f, 1f);
    }
}

}

Hi!

Unity are saying to you that you are trying to access Transform component when there is no such. As you said it happens after the killing of an enemy. So what happens is:

You destroy GameObject therefore your Transform (I assume it’s your EnemyGFX) now is null, because you destroyed enemy, but in FixedUpdate you are still trying to update it’s localScale. However it cannot be access anymore since you destroyed GameObject.

To avoid that issue either add checking before applying, like

if(EnemyGFX != null)

Or try to re-design ur code in such way that after destroying GameObject your script won’t try to access it’s transform. That can be achieved differently, for example if you place script on GameObject itself then after destroying the script also will be destroyed and no longer executed, so the solution depends on your game design.

This is it :

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class Enemy : MonoBehaviour
{

public int health = 100;

public GameObject deathEffect;

public void TakeDamage (int damage)
{
    health -= damage;

    if (health <= 0)
    {
        Die();
    }
}

void Die()
{
    Instantiate(deathEffect, transform.position, Quaternion.identity);
    Destroy(gameObject);
}

}