- Home /
Crash - OnBecameVisible OnBecameInvisible & Mesh distorsion
Hi guys, I have a Crash issue.
I'm twisting meshes at runtime in my application so it fits to a curve that I drew in the editor. Here's an image of what's happening with a few comments :
On the top-left side, you got a prototype (a railroad in this example). On the top-right side, there is some piece of network I drew. And at runtime, you got the result from the bottom of the image.
I used to have those objects drawn in a Manager that looped over them every few frames to check if it had to delete/draw something. But I wanted to optimize my code using the 'OneBecameVisible' & 'OnBecameInvisible' methods because I'm starting to have huge number of objects to draw and I spend more time wondering which I should draw/delete than actualy drawing/deleting anything.
So the idea is that each prototype that must be drawn and twisted along the network, there is a box (an invisible stretched cube) that bounds the space in which the prototype is gonna be placed. Also I'm using a "IsBusy" bool to avoid concurrent drawing/deletion, etc.
So how this is drawn at runtime :
Everytime the box triggers the OnBecameVisible :
While(IsBusy), wait. Set IsBusy to true, instantiated prototype (or get it from a pool if any), compute the vertices distorsion, place the mesh, set back IsBusy to false.
Everytime the box triggers the OnBecameInvisible :
While(IsBusy), wait. Set IsBusy to true, deleted the object (or release it in a pool for further usage), set back IsBusy to false.
Everything works fine as long as you don't try to turn back several times or move forward and backward "too" fast. But when you do, it seems that the OnBecameVisible & OnBecameInvisible triggers in a way that makes my editor crash. I made some tests using only one prototype and I can make my editor crash nearly every time I turn back several times quickly. I added logs everywhere to track down if I didn't messed up my logic with my IsBusy boolean, everything looks cool... I also tried adding logs to track down a potential null ref (even though I don't think this could have lead to a crash), still nothing...
I'm out of options in my code. I'm suspecting the OnBecame(In)Visible methods to be the cause of my crashes. Does anyone have a lead ?
Sorry for that wall of text, I wanted to be clear and I find it quite hard to be clear and concise. Sorry for my english as well.
EDIT : Okay, I never had this before, it seems the editor crashes even when I turn back slowly... I'm even more confused...
EDIT 2 : I migrated my code to the Update function so that the OnBecame(In)Visible methods are not used anymore. Same result. I'm starting to think this is scene/cache related as I never had those troubles before...
EDIT 3 : Ok, fixed it. My code was working perfectly fine. The bug I wanted to correct appeared when an intern of ours added some "fix" in my code. Somehow, he managed to destroy my whole test scene & test prototypes. Even when I reverted his modifications, I could not make anything work again. I'm gonna break his both hands tomorrow, this won't happen again.
Is it a viable solution to loop over your pieces of geometry and perform an AABB test with the camera frustum ins$$anonymous$$d?
Sadly, no. This is the option i'm trying to replace. I used to do this but in the end I was spending my time looping over like 10000 objects just to know "should I draw or delete this object".
Your answer
![](https://koobas.hobune.stream/wayback/20220613182903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use isVisible or OnBecameInvisible() to detect player offscreen 1 Answer
Is there a way that I can control the maximum number of Visable Skinned Mesh? 0 Answers
OnBecameVisible() called out of nowhere... 1 Answer
Onbecomevisable, straight up dosnt work. activates but gameobject is not in camera view? 0 Answers
My Enemy isn't moving! 1 Answer