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 /
avatar image
0
Question by OmryH · Jun 09, 2019 at 04:34 PM · collision3d

Script appears as null and not null at the same time

Hi,

I'm working on a simple 3d shooter that has pillars that disappear if the player tries to hide behind them for too long.

To do this, I detect the presence of the player next to the pillar and if it exceeds a certain amount of seconds, the pilar fades out. Once the player moves away, the pillar returns.

Because my player has many different collisions to detect, I use one script to control all collisions and triggers. Once the collision happens, and time has passed, an event is sent and pillar fades:

     private void OnCollisionStay(Collision collision)
     {
         if (collision.gameObject.tag == "AlertTouch")
         {
             if (collision.gameObject.transform.parent.gameObject.tag == "Pillar")
             {
                 GameObject pillar = collision.gameObject.transform.parent.gameObject;
                 Pillar pillarScript = pillar.GetComponent<Pillar>();
                 alertTouchTime += Time.deltaTime;
                 if (alertTouchTime >= pillarScript.disappearTime)
                 {
                     alertTouchTime = 0;
                     OnPillarFade.Invoke(0, false, pillar);
                 }
 
             }
         }
     }

This worked great until recently when I started getting error messages: "MissingReferenceException: The object of type 'Pillar' 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."

type Pillar is the script attached to the pillar that has the listener for the event:

     private void Update()
     {
         Debug.Log("From update: " + this);
     }
 
     void HandleFade(float fadeValue, bool colliderEnabled, GameObject pillar)
     {
         Debug.Log("From fade " + this);
         if (pillar == this.gameObject)
         {
             myRenderer.material.DOFade(fadeValue, fadePerSecond);
             myCollider.enabled = colliderEnabled;
         }
     }

I check (pillar == this.gameObject) because there are multiple pillars that all listen to the collision and I need to make sure this is the pillar in question.

From the two separate Debug.log calls I get:

"From update: Pillar(Clone) (Pillar) UnityEngine.Debug:Log(Object) Pillar:Update() (at Assets/Scripts/Obstacles/Pillar.cs:24)"

AND

"From fade: null UnityEngine.Debug:Log(Object) Pillar:HandleFade(Single, Boolean, GameObject) (at Assets/Scripts/Obstacles/Pillar.cs:29) PlayerCollisionDetection:OnCollisionStay(Collision) (at Assets/Scripts/Utilities/PlayerCollisionDetection.cs:40)"

at the same time. How can this even happen? I'm not destroying the pillar at any point, not enable/disable anything.

I would appreciate any help, and thanks in advance

Comment
Add comment · Show 1
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 SirPaddow · Jun 09, 2019 at 10:12 PM 0
Share

On which line is the $$anonymous$$issingReferenceException thrown?

Did you try with only one Pillar instance in your scene to make sure that your 2 logs are actually co$$anonymous$$g from the same object?

Are you sure you didn't use "Pillar" and "AlertTouch" on other objects that might not have the Pillar $$anonymous$$onoBehaviour?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 10, 2019 at 09:00 AM

We don't see how you actually call your "HandleFade" method. My Guess is you just do something like

 collision.collider.GetComponent<YourScript>().HandleFade(xxx, yyy, zzz);

Now when GetComponent does not find any component it will return a fake null object when you test inside the editor. In a build an actual null value will be returned. So while you can actually call and enter the method when testing in the editor, you would get a null reference exception in a build.


So make sure you actually look for the component at the right spot and that you actually get a valid component back from GetComponent. For more information on fake null objects, see this blog post.


Note that fake null objects are normal objects from the C# / .NET point of view. However since components (and all UnityEngine.Object derived classes) have a native code counterpart in the C++ engine core having just the managed wrapper class instance is pointless without the actual representation on the C++ side. When you call Destroy on a gameobject you will only destroy the native part. The managed part has to be garbage collected once there's no reference to it anymore.


All Unity API related properties and methods will check if the object is still alive (has its native representation in the engine) and throw a MissingComponentException if it has not. Since you only call pure managed methods Unity won't detect this mistake.


Show us your OnCollisionStay method and we can tell you where it might went wrong.

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 OmryH · Jun 10, 2019 at 10:01 AM 0
Share

The HandleFade is called through OnPillarFade.Invoke(0, false, pillar); inside the collision

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

185 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 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

How can the player position updates randomly when the player isn't movingl? 0 Answers

OnCollisionExit problem 1 Answer

3d Collision for 2d objects 1 Answer

3D enemies keep sliding on the terrain 1 Answer

Jump on the ground 2 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