Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
3
Question by $$anonymous$$ · Oct 14, 2017 at 08:37 PM · gameobjectnullif statementmissingreferenceexception

if (Something != null) not good enough

Hello, it seems that I have been having a problem multiple times where I might have a GameObject variable called "Target" which may or may not exist, so I first check by doing:

 if (Target != null){
 //Something that requires Target to not be null will happen;
 }

This is apparently not good enough though, because my game will randomly crash sometimes when apparently my check is ignored, and I get this error:

MissingReferenceException: The object of type 'GameObject' 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. StatsManagerScript.Update () (at Assets/Scripts/StatsManagerScript.cs:52)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by FlyingHighUp · Oct 14, 2017 at 10:07 PM

Object.Equals is overridden by the UnityEngine, and it checks for destruction.

So instead of Target != null go Target != null && !Target.Equals(null)

Variables don't become null once Unity destroys an object. Internally the C# object still exists until it is no longer referenced, then it is garbage collected.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 14, 2017 at 10:17 PM 1
Share

This is usually not required as long as the variable type is a type derived from UnityEngine.Object.

imagine those variables:

 GameObject obj1 = new GameObject();
 UnityEngine.Object obj2 = obj1;
 System.Object obj3 = obj2;
 
 DestroyImmediate(obj1);
 
 if (obj1 == null) // true
 
 if (obj2 == null) // true
 
 if (obj3 == null) // false

He said the Target variable is of type GameObject in which case the overridden == and != operator is used. Also a nullreference exception doesn't crash the game, it just ter$$anonymous$$ates the current callback execution. The question isn't really clear. It would be great if we can actually see the declaration of the variable and if he would be more clear what he mean by crash

avatar image Matheuz · Apr 07, 2020 at 07:37 PM 0
Share

As @Bunny83 alreayd said, the second check is not necessary for objects of classes that inherit from UnityEngine.Object because that class implements custom == and != operators that not only check for null, but also check if the underlying object has been destroyed. Notice that your $$anonymous$$onoBehaviour might still live even after you've called Destroy on it. That happens because the lifetime of Unity native components are deter$$anonymous$$ed by Destroy calls, but the lifetime of C# scripts is deter$$anonymous$$ed by the garbage collector. Thus, a $$anonymous$$onoBehaviour might still exist when its underyling components (transform, game object...) have been destroyed.

This article explains it more throughly .

avatar image
0

Answer by atulvi · Dec 16, 2020 at 11:26 AM

Any Unity Component Check null or not.

1) Create Script : InternalComponentExtension.cs

 public class InternalComponentExtension : MonoBehaviour
          {
              [SerializeField]private static GameObject _gameObject;
              [SerializeField]public static GameObject GameObject
              {
                    get{
                            if(_gameObject == null)
                           {
                                GameObject g1 = new GameObject();
                                _gameObject = g1;
                           }
                            return _gameObject;
                       }
                 }
          }


2) Access or Check Component Create Script : Demo.cs and add in to any GameObject in scene.

  public class Demo: MonoBehaviour
          {
           
          [SerializeField]private GameObject myGameObject;
          [SerializeField]private GameObject MyGameObject
         {
              get
                  {
                       if(myGameObject == null)
                      {
                            myGameObject = InternalComponentExtension.GameObject;
                      }
                     return myGameObject;
                 }
         }
      void Start()
      {
            //Access any GameObject property.
            MyGameObject.name = "ATUL PATEL";
      
      }
      }





Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Din0m1te · Feb 05, 2021 at 12:52 PM 0
Share

Not sure what this code is supposed to do but it will most definitely produces unexpected behaiour and is simply wrong.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use of if(Component) 1 Answer

MissingReferenceException: The object of type 'Animator' 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. 2 Answers

NullRefrenceException: Object refrence not set to an instance of an object. 0 Answers

Can I check if this.gameObject was destroyed? 5 Answers

How to destroy only the collided instance of prefab and not the original one? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges