- Home /
OnBecameVisible not working on Instantiated object
During the scene load, I instantiate a new GameObject from my Resources directory. (This loads the object just fine in my scene). I then add a component (script) with: obj.AddComponent<ScarabScript>();
In the script file, I know it's attaching correctly since the void Start()
and void Update()
both give output when I have debugging statements within them. However, the private void OnBecameVisible()
function never triggers, even when the object is in clear view in the scene.
What's more odd is I have the exact same script (in a different file) setup for another object I Instantiate earlier in the New Scene function that works just fine. I can't figure out why it won't link up or ever trigger. Is there a better or alternative solution?
Answer by Kyle_WG · Oct 06, 2015 at 04:37 PM
If you're instantiating it, can you not use OnEnable();
or Awake();
functions to do your tasks? This function is relating to the rendering of the object and is worth noting that it is affected by all camers including the scene cameras. Not so sure you can rely on it being called if it's instantiated into view as it quite possibly doesn't call OnBecameVisible();
as it wasn't even alive yet.
Try putting it out of view and back in again to see if it's called then. If it does call then you might need to instantiate it off screen first or use the other methods.
I'm guessing it's the same setup as the one that does work but make sure that your script is attached to the same object as the renderer.
Neither of these seemed to work. The object I'm instantiating is a .blend project file (scarab_beetle.blend). The other I tried is also a .blend project file (key.blend), and if I change the object from scarab_beetle
to key
, then everything works fine. This leads me to believe there might be something fishy with the import of my scarab_beetle.blend object...
Yeah. If one works of the same type then that has to be the issue! Hope you can reexport it. Try converting it to a different type too. Does the beetle render in scene view alright too? (if you just drag the object into the scene)
Definitely something wrong with my scarab_beetle.blend file. Not sure what, but I created a new file from scratch (just a sphere with a single-color texture map), and that's working well, even with the ScarabScript
added to it. Looks like I'll just re-do my scarab beetle...wasn't sure I was happy with the first one anyway.