Forum rules - please read before posting.

How to stop GameCamera2DDrag being destroyed between scenes

Hi everyone. I am making a game in which GameCamera2DDrag is my Default Camera. When I change to a new scene I get the following error:

MissingReferenceException: The object of type 'GameCamera2DDrag' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Component.GetComponent[T] () (at <6afd1274f120405096bc1ad9e2010ba6>:0)
AC._Camera.get_Camera () (at Assets/AdventureCreator/Scripts/Camera/_Camera.cs:295)
AC.GameCamera2DDrag._Update () (at Assets/AdventureCreator/Scripts/Camera/GameCamera2DDrag.cs:120)
AC.StateHandler.Update () (at Assets/AdventureCreator/Scripts/Game engine/StateHandler.cs:278)

I have tried:

  • adding a Constant Id to the camera object
  • attaching this simple script to the camera object:

    using UnityEngine;

    public class DontDestroy : MonoBehaviour
    {
        private void Awake()
        {
            DontDestroyOnLoad(gameObject);
        }
    }
    

Whatever I do I can't seem to stop the camera from being destroyed. When using GameCamera2D in the past I have not encountered this error.

I also have this script attached to my camera to allow mouse-wheel zooming:

using UnityEngine;
using AC;

public class mouseWheelZoom : MonoBehaviour
{

    public float minDistance = -10f;
    public float maxDistance = 10f;
    public float zoomSpeed = 10f;
    public float accelaration = 10f;

    private _Camera associatedCamera;


    private void Awake()
    {
        associatedCamera = GetComponentInParent<_Camera>();
    }


    private void LateUpdate()
    {
        if (KickStarter.mainCamera.attachedCamera != associatedCamera) return;

        float input = Input.GetAxis("Mouse ScrollWheel");

        float currentDistance = associatedCamera.Camera.orthographicSize;
        float targetDistance = currentDistance - (input * zoomSpeed * Time.deltaTime);
        float newDistance = Mathf.Lerp(currentDistance, targetDistance, accelaration * Time.deltaTime);

        newDistance = Mathf.Clamp(newDistance, minDistance, maxDistance);

        associatedCamera.Camera.orthographicSize = newDistance;
    }


}

How can I stop the camera from being destroyed bewteen scenes? Currently I lose the ability to zoom and pan whenever I leave the first scene. Thanks in advance for any help, I've been banging my head against this for days! :(

Comments

  • Update :

    I have discovered that if you make the Camera Object a root object in the hierarchy, then the simple 'DontDestroy' script works. Hopefully this is a good workaround and won't cause any problems later on.

  • My sincere apologies - this is an issue with the current release, and an official update to address it will be out shortly.

    In the meantime, a workaround can be found here.

  • That's great, thank you. I applied the workaround and removed the DontDestroy script from my camera and everything is working as it should.

    No need to apologise! I love this software and appreciate the quick responses on this forum.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.