- Home /
how can I check if an object is null?
in myconsole i keep geting this error when a dragon(with the name of cube) gets destroyed()
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.
i was reading this post and from what i understand i would change it to this?
if (Transform) transform(Rect(0,0,500,500),transform);
is this wrong ...or what is the proper way to do this?
having the same problem over here var enemies: GameObject;
function OnDestroy () { enemies.SetActiveRecursively(true);
if(gameObject==null){
Destroy(this);
}
else;
}
Answer by Eric5h5 · Jun 17, 2011 at 06:45 AM
Yes, that's wrong; the correct way is to refer to the object, not the type.
var someObject : Transform;
function Start () {
if (someObject != null) {
// do something
}
}
ohhhhh ok i see when its asking about transform its really talking about the object that got deleted .....thank you so much8)
i added this script to the dragon but it still shows the same message...y?
What I wrote is an example. You have to write correct code for your own circumstances.
Answer by FriedrichWessel_Inno · May 17, 2018 at 05:27 PM
For future reference:
Unity overloads the == operator. Thats why if(gameObject == null)
will return false even though the object is destroyed ( but not collected by the GC )
The only safe way to do this check is avoid the == operator and use the ?? instead - that one is not overwritten and plain C#
if(gameObjectToTest??false){
// this will only execute if gameObject is not destroyed
}
Detailed information can be found here : https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/
I believe the == operator does exactly the opposite of what you've described. Unity's implementation of the == and != in the UnityEngine.Object class checks if the underlying engine components (transform, gameObject) were destroyed. In that case, a check like if (gameObject == null) Should return null for an object that has been destroyed. Notice that the exception that was thrown was a $$anonymous$$issingReferenceException and not a NullPointerException. The object isn't null, but its reference to the engine component (on the engine's native side) gos destroyed.
Answer by atulpateltmspl · Mar 21, 2020 at 09:41 AM
public ItemContent itemcontent;
// Object Declartion and make a class
// ItemContent Just Check object is null or not..simply
if(itemcontent != null) {
Debug.Log("item content is notnull");
} else {
Debug.Log("item content is null"); }
// -----------------------OR--------------------------------
if( itemcontent == null)
{
Debug.Log("item content is null");
} else {
Debug.Log("item content is not null"); }
========================================
Answer by Thunderkilll · Sep 10, 2020 at 01:18 PM
I think you have to use DontDestroyOnLoad(gameObject);
in this way your game object will not get destroyed . Use this when the script loads ( void start()) I hope this is usefull in any way
Your answer
Follow this Question
Related Questions
MissingReferenceException Help 1 Answer
script trying to access a null gameobject's collider... can't fix 2 Answers
Best practice handling deleted object refences during SceneManager.LoadScene 0 Answers
Expand script to stop camera from following player 2 Answers
How Do Camera Follow Mullti Playeres 2 Answers