- Home /
The object of type 'Transform' has been destroyed but you are still trying to access it.
using UnityEngine;
using System.Collections;
public class AlliedMovment : MonoBehaviour
{
Transform enemy;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
UnityEngine.AI.NavMeshAgent nav;
bool isDead;
void Awake()
{
enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Update()
{
if (GameObject.FindGameObjectWithTag("Enemy") == isDead)
{
Destroy(enemy);
enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
else
{
nav.SetDestination(enemy.position);
}
}
}
Hello, I'm a student with the assignment to modify the Survival Shooter Tutorial. I thought it would be cool to make some of the units friends with the player and help it fight. To make this happen I'm trying to make a script that tells a friendly unit to seek out an enemy, fight it, and then find a new enemy to repeat the process.
I have been trying to figure this out for the past two days and have made little progress. It technically works, except after a unit kills an enemy it will just stand there and will not seek a new target. The console returns an 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.get_position () AlliedMovment.Update () (at Assets/Scripts/Ally/AlliedMovment.cs:26)
"
I have tried to use Destroy(gameObject); to delete the reference, but I couldn't seem to implement it correctly. I also can't seem to figure out a way to tell the friendly unit to seek out the nearest enemy unit. They tend to chase what was closest to them at the time and chase it to the ends of the earth.
I have searched many topics with the same error message, but I have not had luck translating it into my own problem.
I clearly still have a lot to learn, so I was hoping if anyone could please explain what I'm doing wrong or suggest a better way to implement the code?
Answer by LCStark · Sep 30, 2018 at 11:31 AM
If you use Destroy(enemy)
, you are only destroying the Transform
component, meaning the Enemy
object is still there. Your code then finds this object and tries to use its transform, but since it has been destroyed, you're getting the error.
If you use Destroy(gameObject)
, you are passing the GameObject
assigned to your AlliedMovment
object, which would destroy the wrong object.
Try using Destroy(enemy.gameObject)
. This should get rid of the entire Enemy
game object.
EDIT
Also, I think that the Destroy
doesn't destroy the object immediately, so since you're searching for a new object immediately after "destroying" the previous one, you'll still probably find the same object. Try modifying your code so when you destroy your current enemy you set your enemy
reference to null
, and at the beginning of your Update
function you add:
if (enemy == null) {
// find new enemy
}
Your answer
Follow this Question
Related Questions
How can I debug gameObject destruction? 2 Answers
Unity projects not working Error"could not establish a connection with unity" 1 Answer
Playing particle on 1 instance of cloned prefab 0 Answers
Problem with collisions 0 Answers
Optimize huge number of Destroy() calls (cannot use object pooling) 3 Answers