- Home /
How do I check if an object is null?
I am making a game where enemies chase their target by moving towards them and rotating their gun towards them, but whenever the player is destroyed I get this 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.
This is the rotate gun script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class rotateEnemyGun : MonoBehaviour { public float offset; public Transform player;
// Update is called once per frame
void Update()
{
if (player != null)
{
Vector2 playerPos = player.transform.position;
Vector2 lookPos = playerPos - (Vector2)transform.position;
float rotZ = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(rotZ - offset, Vector3.forward);
}
}
}
Answer by Bunny83 · Mar 19, 2020 at 01:14 AM
I don't see any issues with the code you've posted. Are you sure the error is in this script? The code you've posted can't really be the cause of the error you've mentioned.
There is no error in the script, the error is that when the player gets destroyed by the bullets from the enemy, it is trying to find the non existing player, but it can't find it, so it gives the error $$anonymous$$issingReferenceException: 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.
You just contradicted yourself in your own paragtaph. Unity does never "try to find an object of type Transform" by it's own. It has be your code that does that and would cause a runtime exception if it fails. As I said the code you posted can't cause this issue because the error you see can only happen when an object that is referenced by a variable got destroyed. Your "not null" does catch destroyed objects properly so it can not enter the code where you try to access the transform.
When you get an error in the console during play you actually get a detailed stacktrace which tells you exactly where the error happened. It tells you the exact file and even the line number. You get that error, not us. You haven't included that information in your question so we can't tell you what your issue is. All I can tell you is that the code you've posted can not cause this error.
So again are you sure the error comes from the script you've posted? Can you include the stacktrace from the error you got? Note that you can select and copy text from the bottom half of the console window.
Oh ok, I'll look in to that. I'll send you the full error: $$anonymous$$issingReferenceException: 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 () <0x24abe250e90 + 0x0006a> in :0 EnemyAI.Update () (at Assets/Scripts/Enemy/EnemyAI.cs:26)
Answer by niklasdette · Mar 19, 2020 at 12:22 AM
It could be the following reason, but not 100% sure:
You are checking if the player is "null". But you are deleting the player. So the player is not "null" it just has a "missing reference" (you should be able to see it in the inspector). So you should maybe set the player manually to "null" before destroying it.
I hope it works :)
Create a refence to the script where you are destroying it.
In this case you would create a variable in the script named:
public rotateEnemyGun variableName; //This you would assign of course for example in the inspector per trag and drop
and then before you are destrying the player: variableName.player = null;
Doesn't look like it worked, thanks for trying.
Answer by The-Burgunfaust · Mar 19, 2020 at 03:52 PM
Don't destroy the player, just disable it.
I would have to agree with The-Burgundaust, Unless your game finishes and you cant play again after you die until you restart the game (restart meaning close application and reopen), then destroying the character is fine, but if you want lives, or if you want to move the character back to the start, then defintly just disable it, destroying it means exactly that.. its destroyed and you can no longer use it. Destroying gameobjects is for objects that you never want to use again during that run-time.. so if you do want to use the charcter after it has died do not destroy it, just turn it off for the time being.
I know this comment was a year ago, but how exactly do I disable it. I'm running into the exact same issue, but I can't quite find a Disable command.
Thank you!
The way to disable game objects is:
gameObject.SetActive(false);
And the below will turn the object back on
gameObject.SetActive(true);
Answer by Cyber-Spyder · Mar 19, 2020 at 12:12 AM
@Jasic560 Try If (player = null) { return }
that might fix it
Answer by Xpartano · Mar 19, 2020 at 12:57 AM
Have you tried if (player)
instead of if (player != null)
?
Your answer
