Camera works fine in the editor, but doesn't when build

I have a camera that follows the character, and it works fine in the editor, but when I make the build, the camera doesn’t move at all.

The cod eis below, but overall, the camera has two modes: one that follows the characters, but must be inside a collider, so at the start and end of the level, the character won’t be in the center. The second mode make the camera go from it’s position to the character. This is for the instances where the camera stops for a combat encounter, and then goes back to the character.

The camera has some triggers as children, but I removed them and the camera still doesn’t move when making the build. Again, it works perfectly in the editor. This camera has been copied from another scene, and I think I copied it back, so it isn’t a fresh original camera. I created a new one but again, not moving at all.

I don’t see how the code can be wrong if it works in the editor. I also tested using only one of the camera modes to see if one is broken in some way, but again, works in the editor, no when build. I also don’t think I changed anything important in the build options. Even reseting the build options won’t change a thing.

Anyway, this is the code for the camera:

public Transform player;
    public Vector2 margin;
    public Vector2 smoothing;
    public BoxCollider2D limites;
    private Vector3 max;
    private Vector3 min;
    private Vector3 velocity = Vector3.zero;

    private Camera camara;
    public bool activo = true;

    // Transforms to act as start and end markers for the journey.
    public Transform startMarker;
    private Vector3 start2;
    private Vector3 end2;
    public bool goTo;
    // Movement speed in units per second.
    private float speed = 0.08F;

    // Time when the movement started.
    private float startTime;

    // Total distance between the markers.
    private float journeyLength;

    // Use this for initialization
    void Start () {
        max = limites.bounds.max;
        min = limites.bounds.min;
        camara = GetComponent<Camera>();
        player = GameObject.FindWithTag("Player").transform;

        // Keep a note of the time the movement started.
        startTime = Time.time;

        // Calculate the journey length.
        startMarker = gameObject.transform;
        journeyLength = Vector3.Distance(startMarker.position, player.position);
    }
   
    // Update is called once per frame
    void Update () {
       
        if(activo)
        {
            moverse();
        }  

        //prueba2
        if(goTo)
        {
            // Distance moved equals elapsed time times speed..
            float distCovered = (Time.time - startTime) * speed;

            // Fraction of journey completed equals current distance divided by total distance.
            float fractionOfJourney = distCovered / journeyLength;

            // Set our position as a fraction of the distance between the markers.
            start2 = new Vector3(startMarker.position.x,0,-10);
            end2 = new Vector3(player.position.x,0,-10);
            transform.position = Vector3.Lerp(start2, end2, fractionOfJourney);

            if(transform.position.x == player.position.x) {
                goTo = false;
                activo = true;
            }
        }
       
    }

    public void moverse()
    {
        //Debug.Log(player.transform.position);

            Vector3 current = transform.position;

            float x = transform.position.x;
            //float y = transform.position.y;


            //if (Mathf.Abs(x - player.position.x) > margin.x)
            //{
                x = Mathf.Lerp(x, player.position.x, smoothing.x * Time.deltaTime);
            //}

            //if (Mathf.Abs(y - player.position.y) > margin.y)
            //{
                //y = Mathf.Lerp(y, player.position.y, smoothing.y * Time.deltaTime); esta linea sola estaba activa
            //}
           

            float cameraHalfWidth = camara.orthographicSize * ((float)Screen.width / Screen.height);

            x = Mathf.Clamp(x, min.x + cameraHalfWidth, max.x + cameraHalfWidth);
            //y = Mathf.Clamp(y, min.y + camara.orthographicSize, max.y + camara.orthographicSize);

            //transform.position = new Vector3(x, 0, transform.position.z);
            //Vector3 target = new Vector3(0, y, transform.position.z); //esta lina estaba activa
            Vector3 target = new Vector3(x, 0, transform.position.z);

            //smooth
            Vector3 newPos = Vector3.SmoothDamp(current,
                                                target,
                                                ref velocity,
                                                0f);
           
            transform.position = newPos;
    }

The editor and builds are not completely the same, there are many instances where things can behave differently. Because of this, it’s important to regularly test your game in builds and on devices.

The order of events and Unity messages can e.g. be different in builds and break things that inadvertently depend on the order in the editor. Or you can get an exception somewhere, which that then can break other things.

You’ll have to debug your build. First check the player logs for any errors (you can also attach the editor console to debug builds) and then place logs in your code to figure out what is working, what isn’t and where exactly it breaks. You can also connect a debugger, which requires some setup but can be quicker once you’re attached to a build.

Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

There’s even a dedicated forum: Unity Engine - Unity Discussions

Okay, so I reviewed the Player.log file and there were plenty of errors.

Then I installed Cinemachine, and got it working pretty fast, so I removed the Camera script because I don’t need it anymore. I’m also using a confiner, and it works great (I have to change a script that stops the camera but I’ll leave for later).

But the problem now is similar… it works great on the Editor, but when building, it seems that every change to the cinemachine options are gone, it doesn’t even respect the confiner. At least it follows the character, even though not as I want. I checked the log and there are no errors at all.

So I remade the entire scene from scratch. Well, I did it for the camera, everything else was copy-pasted, and it seems to work well now. So I’m guessing that copy-pasting cameras is something to avoid. Thank you for the ideas anyway, the Cinemachine cameras seem like a huge step up.