: The object of type ‘Transform’ has been destroyed but you are still trying to access it.

Updated on April 21, 2017 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on August 22, 2016

I copied the code from Brackeys 2d platformer course. The game stops working every time I die. The Enemy AI doesn’t seem to know what to follow after I die. I don’t know how to solve this problem. It only gives me the error above.
using UnityEngine;
using System.Collections;
using Pathfinding;
[RequireComponent (typeof (Rigidbody2D))]
[RequireComponent (typeof (Seeker))]
public class EnemyAI : MonoBehaviour {
 // What to chase?
public Transform target;

// How many times each second we will update our path
public float updateRate = 2f;

// Caching
private Seeker seeker;
private Rigidbody2D rb;

//The calculated path
public Path path;

//The AI’s speed per second
public float speed = 300f;
public ForceMode2D fMode;

[HideInInspector]
public bool pathIsEnded = false;

// The max distance from the AI to a waypoint for it to continue to the next waypoint
public float nextWaypointDistance = 3;

// The waypoint we are currently moving towards
private int currentWaypoint = 0;
private bool searchingForPlayer = false;

void Start () {
seeker = GetComponent();
rb = GetComponent();
if (target == null) {
if (!searchingForPlayer) {
searchingForPlayer = true;
StartCoroutine  (SearchForPlayer());
            }
return;
}

// Start a new path to the target position, return the result to the OnPathComplete method
seeker.StartPath (transform.position, target.position, OnPathComplete);
StartCoroutine (UpdatePath ());
}

IEnumerator SearchForPlayer () {
GameObject sResult = GameObject.FindGameObjectWithTag(“Player”);
if (sResult == null) {
yield return new WaitForSeconds ( 0.5f );
StartCoroutine    (SearchForPlayer());
} else {
target = sResult.transform;
searchingForPlayer = false;
StartCoroutine(UpdatePath());
yield return false;
}
}

    IEnumerator UpdatePath() {
if (target == null)
{
if (!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
            }
yield return false;
}
// Start a new path to the target position, return the result to the OnPathComplete method
seeker.StartPath(transform.position, target.position, OnPathComplete);
yield return new WaitForSeconds ( 1f/updateRate );
StartCoroutine (UpdatePath());
}

public void OnPathComplete (Path p) {
Debug.Log (“We got a path. Did it have an error? ” + p.error);
if (!p.error) {
path = p;
currentWaypoint = 0;
}
}

void FixedUpdate () {
if (target == null)
{
if (!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
            }
return;
}
        //TODO: Always look at player?
        if (path == null)
return;

if (currentWaypoint >= path.vectorPath.Count) {
if (pathIsEnded)
return;

Debug.Log (“End of path reached.”);
pathIsEnded = true;
return;
}
pathIsEnded = false;

//Direction to the next waypoint
Vector3 dir = ( path.vectorPath[currentWaypoint] – transform.position ).normalized;
dir *= speed * Time.fixedDeltaTime;
//Move the AI
rb.AddForce (dir, fMode);
        float dist = Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]);
if (dist < nextWaypointDistance) {
currentWaypoint++;
return;

}
}

}

  • Liked by
Reply
0 on August 22, 2016

The player’s Transform have been destroyed but the enemy is still looking for him.

  • Liked by
Reply
Cancel
0 on March 18, 2017

i also have the same problem as his, anyone out there who can help ? PLEASE 🙁

  • Liked by
Reply
Cancel
0 on March 20, 2017

I too face the same error in many projects which I learned and created

  • Liked by
Reply
Cancel
0 on April 20, 2017

could anyone fix that? i have same problem too;

seeker.StartPath (transform.position, target.position, OnPathComplete);

yield return new WaitForSeconds ( 1f/updateRate );
StartCoroutine (UpdatePath());

it says problem is here. i couldnt figure that out

  • Liked by
Reply
Cancel
0 on April 21, 2017

The error simply means that since the target transform is gone, the enemy script doesn’t know what object to look for. I don’t really see how this is that much of a problem as the player is dead, but if you want to fix it I would suggest checking if the player exists before telling the enemies to look for the player in either the SearchForPlayer() function or the UpdatePath() function.

  • Liked by
Reply
Cancel