Automatic background change

My friend started learning unity and he wanted to make it so every 30 seconds or so , a random background different to the one currently set, would teleport behind the current one, and the current one fades so it looks like it fades to a new background. It would’nt be that hard we thought…
Here is that code…

using UnityEngine;
using UnityEngine.UIElements;

public class Camera_Script : MonoBehaviour
{
    public int randomN;
    public GameObject firstBackground;
    public GameObject seacondBackground;
    public GameObject tirstBackground;
    public float startTime = 34f;
    public float curenttime = 0f;
    public int randomNChangeTo;
    public Animator PlayGhost1;
    public Animator PlayGhost2;
    public Animator PlayGhost3;
    public Vector3 Offcamera;
    public Vector3 Oncamera;
    public Vector3 NewBackground;

    void Start()
    {
        Offcamera = new Vector3(50, 0 , 0);
        Oncamera = new Vector3(0, 0, 0);
        NewBackground = new Vector3(0, 0, 1);
        curenttime = startTime;
        randomN = Random.Range(1, 4);
        print(randomN);

        if (randomN == 1)
        {
            firstBackground.transform.position = Oncamera;
            seacondBackground.transform.position = Offcamera;
            tirstBackground.transform.position = Offcamera;
        }
        if (randomN == 2)
        {
            seacondBackground.transform.position = Oncamera;
            firstBackground.transform.position = Offcamera;
            tirstBackground.transform.position = Offcamera;
        }
        if (randomN == 3)
        {
            tirstBackground.transform.position = Oncamera;
            seacondBackground.transform.position = Offcamera;
            firstBackground.transform.position = Offcamera;
        }

    }

    void Update()
    {

        curenttime -= 1 * Time.deltaTime;
            if (curenttime <= 1)
            {
            randomNChangeTo = Random.Range(1, 4);
            if (randomNChangeTo != randomN)
                {
                    TransitionBackground();
                    GhostBeginns();
                    if (curenttime <= 0)
                    {
                    randomNChangeTo = randomN;
                    Changebackground();
                    curenttime = startTime;
                    }
                }
            }
    }
    void Changebackground()
    {
        if (randomN == 1)
        {
            firstBackground.transform.position = Oncamera;
            seacondBackground.transform.position = Offcamera;
            tirstBackground.transform.position = Offcamera;
        }
        if (randomN == 2)
        {
            seacondBackground.transform.position = Oncamera;
            firstBackground.transform.position = Offcamera;
            tirstBackground.transform.position = Offcamera;
        }
        if (randomN == 3)
        {
            tirstBackground.transform.position = Oncamera;
            seacondBackground.transform.position = Offcamera;
            firstBackground.transform.position = Offcamera;
        }
    }
    void TransitionBackground ()
    {
        if (randomNChangeTo == 1)
        {
            firstBackground.transform.position = NewBackground;
        }
        if (randomNChangeTo == 2)
        {
            seacondBackground.transform.position = NewBackground;
        }
        if (randomNChangeTo == 3)
        {
            tirstBackground.transform.position = NewBackground;
        }
    }
    void GhostBeginns ()
    {
        if (randomN == 1)
        {
            PlayGhost1.Play("PlayFadeOut");
        }
        if (randomN == 2)
        {
            PlayGhost2.Play("PlayFadeOut");
        }
        if (randomN == 3)
        {
            PlayGhost3.Play("PlayFadeOut");
        }
    }
}

I just don’t know how to explain the issue…
Also maybe a video could help you visualize the issue.
9264zf

We tried fixing it for 4 hours straight…
our brain dead right now

Just a quick glance, and lines 53 and 54 make no sense. Your multiplication on line 53 is meaningless (1 * Something = Something).

But a big problem is so long as curenttime is <= 1 but > 0, it will keep generating new random numbers on line 56 and calling TransitionBackground() & GhostBeginns() each frame, which look intended to be only called once at the start of the transition. You also never change the value of randomN outside of Start.

Your misspellings in variable names is also very distracting, making reading your code more difficult than it should be.

Oh ok. But how would that look in code? I am lost sorry i have no idea.

If you want to do something once from Update instead of doing it over and over every frame, easiest way is to just set a bool saying you already did that thing. Check if that bool is set before doing the same thing again. When the entire transition/fade process is complete, you set the bool back for the next transition.