Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

problem with deleting objects out of view

Discussion in 'Scripting' started by Radiators, Aug 15, 2020.

  1. Radiators

    Radiators

    Joined:
    Aug 13, 2020
    Posts:
    29
    Hello
    I have problem in my 2D game with deleting objects which are instantiated prefabs, i know how to delete them after some amount of time but i need to delete them when they will go out of the screen but my script doing this so quickly when objCan is still visible on the screen
    Code (CSharp):
    1.  
    2. public class destroyObj : MonoBehaviour
    3. {
    4.    private Vector2 screenBounds;
    5.     private objCreator objCreatorRef;
    6.     public GameObject objCan;
    7.     private void Start()
    8.     {
    9.         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    10.     }
    11.     private void Update()
    12.     {
    13.         if(objCan.transform.position.x < screenBounds.x * 2)
    14.         {
    15.             Destroy(objCan);
    16.             objCreatorRef.numberOfMaxCans--;
    17.         }
    18.     }
    19. }
    this code i tried assign to objCan prefab, camera and spawnObject(which spawn prefabs) but always they will show on short time a then they are destroyed even when they are still visible
    i tried everything like renderer or box collider in order to destroy that prefabs but it didnt work...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    What you are doing seems reasonable.

    I suggest before the Destroy() call you insert this code:

    Code (csharp):
    1. Debug.Break();
    This will pause the editor at the moment the item is destroyed. It will still be destroyed.

    However, right there you can also insert additional lines of code to use Debug.Log() and print out values such as
    objCan.transform.position.x
    and
    screenBounds.x
    and see what they are. This should help you reason about why the values are not what you expect.
     
  3. Radiators

    Radiators

    Joined:
    Aug 13, 2020
    Posts:
    29
    i tried check positions and screenBounds works properly but objCan sending zero o_O
     
  4. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    The problem is that you are deleting objects when the pivot reaches the end of the screen, but part of the sprite may be on screen.

    You can either use GetComponent<Renderer>().isVisible or adding the sprite bounds to your method, like Kurt-Dekker already suggested.

    Hope it helps!
     
  5. Radiators

    Radiators

    Joined:
    Aug 13, 2020
    Posts:
    29
    this is not my problem because pivots are on the right place so i tried these scripts and first i tried to assign to prefab,camera and to spawn object in hiearchy but it didn't work and second script telling my that object is still visible even if it is not

    FIRST:
    Code (CSharp):
    1. public class DeletingObjects: MonoBehaviour
    2. {
    3. private bool hadAppeared;
    4. public GameObject CAN;
    5.  
    6.     Renderer renderer;
    7. start()
    8. {
    9. hadAppeared = false;
    10. renderer = GetComponent<Renderer>();
    11. }
    12. void update()
    13. {
    14. if (renderer.isVisible)
    15.         {
    16.             hadAppeared = true;
    17.        
    18.             Debug.Log("CAN IS VISIBLE");
    19.         }
    20.         if (hadAppeared)
    21.         {
    22.             if (!renderer.isVisible)
    23.             {
    24.                 Destroy(CAN);
    25.                 Debug.Log("CAN IS INVISIBLE");
    26.             }
    27.         }
    28. }
    29. }
    SECOND:
    Code (CSharp):
    1. public class DeletingObjects: MonoBehaviour
    2. {
    3. public void OnBecameVisible()
    4.      {
    5.          Debug.Log("CAN IS VISIBLE");
    6.      }
    7.     public void OnBecameInVisible()
    8.     {
    9.         Debug.Log("CAN IS INVISIBLE XXX");
    10.     }
     
    Last edited: Aug 17, 2020