- Home /
how do I get rid of the word transform in my script and destroy a player (Character Controller) without an issue?
My enemy destroyed a player but I've been having an issue with this transform in my script of NavMeshAgent. So how do I get rid of the word transform in my script and destroy a player (Character Controller) without an issue? Or how do I create a null like a Transform player = null, so the player can be destroyed?
Console: "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.
Answer by Namey5 · Apr 07, 2020 at 02:21 AM
You need to check if the player object still exists, i.e.
if (player != null)
{
//Do something...
}
This is a fairly common failsafe that you should use in most situations that work with an instance that might not always exist (or at least be available).
For example, in your enemyChase script;
...
void Update ()
{
if (player != null && Vector3.Distance (destination, player.position) > 1.0f)
{
destination = player.position;
target.destination = destination;
}
}
...