Objects wont delete when they go off screen

Hello, I made this code with the help of a tutorial and tried to add the other sides myself.

the code needs to delete every object that goes off screen but only the top 2 if statements work those are for the top and the right of the screen, but the bottom and the right of the screen won’t work. I use Unity version 2020.3.19f1 and it is a 2D project. Hope someone can help me.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectRemove : MonoBehaviour
{
    private Vector2 screenBounds;

    // Start is called before the first frame update
    void Start()
    {
        screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    }

    // Update is called once per frame
    void Update()
    {  
        if (transform.position.x > screenBounds.x * 1.2) //this one works
        {
            Destroy(this.gameObject);
        }
        if (transform.position.y > screenBounds.y * 1.2) //this one works
        {
            Destroy(this.gameObject);
        }
        if (transform.position.x < screenBounds.x * 1.2) //this one does not work 
        {
            Destroy(this.gameObject);
        }
        if (transform.position.y < screenBounds.y * 1.2) //this one does not work
        {
            Destroy(this.gameObject);
        }
    }
}

Edit: when I use all these 4 if statements the objects get deleted even if they are in the scene.

The way you have done it you have a single point in space, screenBounds.

You have set that point to the upper right of the camera view.

What about the lower left?

I would instead have a screenUpperLeft and a screenLowerRight and then check each appropriately with your positions.

But don’t take my word for it, find out for yourself!

Here’s an easy way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

just because you make the name of the variable screen bounds doesnt mean that its actually the bounds of the screen