c# - (Unity 2D) Destroy instantiated prefab when it goes off screen? -


After the text "itemprop =" text ">

im create unity 2d games in a 2D game (4.3), and i get prefabs that get instantiated when those prefabs go delete Need for the screen off I have written some code to underwent the object, but when I close the screen I want to remove those prefabs. Here's the code I have written so far.

to generate prefab (C #):

  zero update () {float y = Random.Range (-4.53f, 2.207f); If (X & LT; 2000) {Institution (Handicap, New Vector 3 (Y, X * 6.0 F, 0), Quattrocessor); X ++; } // debug Log (x); }   

to destroy prefab (C #):

  / *************** * *************************************************************************************************** * ******************************** InstantIated Obligation Receives * And Destroy It On The Exit * Memory ******** To save ********************************************************************* *** ****************************************** (GameObject) Illustration (obstacle); / * If (clone.transform.position.y == -11) {deleted (clone); Debug.Log ("deleted"); } * / Vector2 screenposition = camera.man Word Toscinpoint (Transport.); If (screen.position.y> Screen.height || screenPosition.y & lt; 0) {destroyed (gameObject); Debug.Log ("destroyed"); }   

However, the code destroying the object is not working, but there is no error even after the prefix screen is shut down, so the output is "destined" I know something wrong with the code to destroy it.

Thanks

You can create an element that will destroy itself When the situation is out of the camera, then attach this component to the constraint.

  Zero Update () {Float Y = Random range (-4.53F, 2.207F); 2000) {Game Object Clone = (Game Object) Institut (Handicap, New Vector 3 (Y, X * 6.0 F, 0), Quatnawn. Clone.AddComponent (typeof (DestroyMySelf)); X ++;}} < / Code>  

and give this component the hindrance to themselves

  Public class DestroyMySelf: MonoBehavior {Zero update () {Vector2 screenPosition = camera. Main.WorldToScreenPoint (transform.position); if (screen.position.y & gt; Screen.height || screenPosition.y & lt; 0) Destroy (this.gameObject);}}    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -