Why does only one of my instantiated scripts work?

(TL:DR "My prefab is supposed to create another one of itself if you keep going in a direction, but it only does this in one direction(the one you go to first), the other direction doesn’t work)

So, I have my first game where a plane flies around

I want some clouds to appear behind the plane but they only generate in front of the plane and get deleted behind it.

I have done this by having a background prefab, a left border, and an instantiate position. The prefab contains the background, the left border, and the instantiate position.

Once the player reaches the left border’s x position, the prefab instantiates at the instantiate position. This works pretty well, however, when I tried to copy and paste the same script (with some changes for the right side), and another border and instantiate position for the right side, it doesn’t work.

It lets me go one direction and it looks like it’s supposed to, but when I go the opposite direction, it doesn’t, it’s weird.

Here is the code for the left side:

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

public class LoopLeftTest : MonoBehaviour
{
    private GameObject empty;
    private GameObject Player;
    private GameObject right;
    private bool dio = true;
    private GameObject bor;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        Player = GameObject.Find("Player");
        empty = GameObject.Find("Empty");
        right = GameObject.Find("Lefto");
        bor = gameObject;

        if (Player.transform.position.x < bor.transform.position.x + 2)
        {
            if (Player.transform.position.x > bor.transform.position.x - 2)
            {
                if (dio == true)
                {
                    Instantiate(Resources.Load("SkyPreFab"), right.transform.position, empty.transform.rotation);
                    dio = false;
                }
            }
        }
    }
}

and here’s the code for the right side:

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

public class LoopRightTest : MonoBehaviour
{
    private GameObject empty;
    private GameObject Player;
    private GameObject right;
    private bool dio = true;
    private GameObject bor;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        Player = GameObject.Find("Player");
        empty = GameObject.Find("Empty");
        right = GameObject.Find("Righto");
        bor = gameObject;

        if (Player.transform.position.x < bor.transform.position.x + 2)
        {
            if (Player.transform.position.x > bor.transform.position.x - 2)
            {
                if (dio == true)
                {
                    Instantiate(Resources.Load("SkyPreFab"), right.transform.position, empty.transform.rotation);
                    dio = false;
                }
            }
        }
    }
}

When I go one direction, it’s supposed to loop, and it does, (one of these scripts keeps activating), but when I go the opposite direction, it doesn’t loop!

Thank you very much
-Spaceface

it’s hard to understand from your description what you are trying to do. It sounds like you want something to wrap from the right edge to the left edge if it moves off the right edge, and the opposite action if it moves off the left. (it is not clear if you are talking about the airplane wrapping around or the clouds!)

So, simplify it. You should only use 1 script - you do not need 2 scripts!!!

As is, the code doesn’t distinguish between hitting the left or right. Here’s the algorithm you should use:

 If it is beyond the left bounds, do your right thing 
 ELSE if it is beyond the right bounds, do your left thing 

you also talk about the problem of clouds instantiating in front of the airplane? If you have a background gameobject that is behind the plane, simply parent the cloud game object to that object immediately after you instantiate it. for example:

GameObject cloud = (GameObject) intantiate( ...... );
cloud.parent = backGroundGameObject;

Also you should not be using GameObject.Find("") to get references. That method is semi heavy as it is by putting it in update it just increases its size. This should happen in start or just slide in references in inspector. And resource.load is heavy. You should have reference of prefab, then just instantiate reference.