• 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
Question by electricear · Jul 02, 2013 at 06:49 PM · c#collisiontrigger

Missing GameObject and Null do not test the same, but how do I test for missing?

I have a grid of Box Colliders that are supposed to sense and monitor other objects in them. The OnTriggerEnter event tests to see if the "occupancy" is Null, and if it is then the block can be occupied by something else. So here is the problem: When the occupancy variable reports "None" (null) new objects occupy the spot with out any problems, but when the object that was in that spot is destroyed the variable reports as "Missing", and reports to the Debug.Log show it as null, but it does not test as null on the OnTriggerEnter event. So my question is How do I test for a random "missing" object, or how do I clear it so it is actually null?

I do not want to bog down my app but having hundreds/thousands of these colliders testing an OnCollisionStay to force a null, nor do I want to have to traverse the object grid unnecessarily with each event call to force a null.

Comment
BlueRaja_2014

People who like this

1 Show 0
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

6 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Dave-Carlile · Jul 02, 2013 at 08:16 PM

 Destroy(block.occupancy);
 block.occupancy = null;
Comment
Stardog
jister

People who like this

2 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 electricear · Jul 03, 2013 at 04:12 AM 0
Share

This would be destroying the object occupancy from the collider grid, and runs into the problem of the object occupying multiple collider points. I suppose I could create a trigger list similar to what I mentioned above, but where the object would tell all it's occupied grid points to null, and after the list was done destroy it self, but this would lack the branching potential of the method I mentioned above... Still if that method fails, I may use this idea as a backup plan.

avatar image

Answer by DriesVienne · Jul 02, 2013 at 08:54 PM

I have no direct answer, but a workaround came to mind.

You could have your occupants reference their occupying collider, and notify the collider when it gets destroyed, thus allowing it to be occupied later by other objects.

Alternatively, depending on how fast objects are added and removed to the grid, and the size of your grid, you could have your occupants BroadcastMessage to your colliders to notify them of it's destruction.

This could work, but it avoids the real question; getting "Missing" when you should be getting null. Someone smarter than me might be able to help with that.

Comment
electricear

People who like this

1 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 electricear · Jul 03, 2013 at 03:58 AM 0
Share

I had thought about using both these methods at an earlier time. The problem I originally had with the first method was that a single object could occupy multiple squares, and I had stored the spaces occupied on the object begin destroyed. Sadly it couldn't traverse a list before the object was missing level of null. more on this below.

The second method is an ok solution, but it means all the colliders are basically monitoring for an additional trigger event which to me seems silly considering what they are.

That being said, After thinking about the problem for a little bit The solution I've come up with, and am hoping will work, is to store the name of the occupancy item on the colliders along with it's occupancy reference. The object will store a reference to one of the colliders it occupies, and when it's destroyed it will have that object set off a chain reaction having those colliders null their occupancy. Thus removing their dependency on the occupancy object being there.

avatar image

Answer by Noztradamuz · Jul 02, 2013 at 10:18 PM

Ok, maybe some code will help us, to help you, as far as i get with this i think the problem is that you're trying to acces a GameObject that's already been deleted (mean Null or Object reference not set to an instance of an object) if this is the case, you should put some checks like this

 OnCollisionEnter(Collision occupancyObject)
 {
   objInHere = occupancyObject.gameObject;
 }
 
 OnCollisionStay(Collision stayingObject)
 {
   if(objInHere!=null && stayingObject.gameObject == objInHere)
   {
     //Do whatever you need to
   }
   else
   {
     objInHere = null;
   }
 }

Maybe this should help.

Comment
Steven-1
Nomadx

People who like this

0 Show 0 · 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

Answer by electricear · Jul 03, 2013 at 04:26 AM

It actually doesn't need code. When I started troubleshooting the problem I set up a null test in the collider objects Update where it first Debug.Log(ged)the Occupancy, then tested for null and simply changed the color of their associated cubes. The variable is public so I could monitor it in the inspector as well. The debug console reported both types as Null, but the inspector listed the null as none where as the destroyed object was just missing. Because the Log came before the color change test, and reported both as null but only the none version worked as I expected. I started trying to track down ways to turn missing to null with out needing the actual missing object to do it.

Comment

People who like this

0 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 ZoopTEK · Oct 21, 2014 at 01:01 AM 0
Share

Did you ever find a solution to this issue? I am having the exact same problem right now. Somehow, saved into my scene, I have a "Missing (Game Object)." Of course I can't select it... because it doesn't exist.

avatar image

Answer by IllogicalGames · Oct 21, 2014 at 02:08 AM

instead of using colliders, use compare range between the objects. its much more lighter than colliders since its not using physics. if you want to optimize it further, use on FixedUpdate.

Comment

People who like this

0 Show 0 · 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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

On Trigger Enter, Collide with object, specific collision 1 Answer

How to enter trigger a certain amount of times before executing code? 1 Answer

Problem Detecting 2D Collisions 1 Answer

Ignore collision of a gameobject that is the child of the same parent? 2 Answers

Animation with frozen player ?? 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