- Home /
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.
Answer by Dave-Carlile · Jul 02, 2013 at 08:16 PM
Destroy(block.occupancy);
block.occupancy = null;
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.
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.
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.
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.
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.
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 "$$anonymous$$issing (Game Object)." Of course I can't select it... because it doesn't exist.
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.