0
\$\begingroup\$

does anyone know how to delete/destoy a spawn object when it exit out of the camera view. I tried using:

   void OnBecameVisible() {
      Destroy (gameObject);
     }

But it didn't work. Maybe I'm doing something wrong, I don't know. Oh just a friendly advice my spawned object may enter form any point of the screen. Thank you :).

\$\endgroup\$
1
  • \$\begingroup\$ Why would you be checking if the object was visible, when it is literally the exact opposite of being not visible? \$\endgroup\$
    – Gnemlock
    Aug 21 '16 at 2:54
4
\$\begingroup\$

Try with OnBecameInvisible instead.

\$\endgroup\$
-3
\$\begingroup\$

Try this:

var obj : Transform;   
if (raycast.transform != obj)
    Destroy(obj.gameObject); 

where obj is your object. This needs to be applied to your camera. This should work.

\$\endgroup\$
6
  • \$\begingroup\$ Thank you, but could you convert it to c# . Please and Thank you \$\endgroup\$ Aug 20 '16 at 20:38
  • \$\begingroup\$ I usally use javascript, but here you go: public Transform //name of object//; if (RaycastHit.transform != //name of object//) { Destroy(//name of object//) } \$\endgroup\$ Aug 20 '16 at 20:50
  • \$\begingroup\$ Thank you, I'll try it out once I fix unity, I'm having a bug \$\endgroup\$ Aug 20 '16 at 20:56
  • \$\begingroup\$ Alright, and make sure to vote for my answer! \$\endgroup\$ Aug 20 '16 at 20:58
  • \$\begingroup\$ @TheMattBat999, this answer confused me. Especially considering a far easier to understand solution was posted not 20 seconds after yours. You have raised, previously, that you are "just trying to help". Since you mean well, I thought I would address that this site favors quality over quantity. You might mean well, but if you keep posting answers that get down-voted, the system actually blocks you from posting any more answers. Test out your material, assume the audience is not "a genius" like you, and consider formatting to make your answers more presentable. \$\endgroup\$
    – Gnemlock
    Aug 21 '16 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.