Camera position depending on Gameobject in prev. scene | UnassignedReferenceException

Hi,
I have 2 scenes: AR and VR.
In the AR scene there are 2 teleporter gameobjects that start the transition to VR. Depending on which teleporter I have selected, I would like to set the camera in VR to a specific position.

public class VRGaze : MonoBehaviour
{
    public Image imgGaze;
    public float totalTime = 2;
    bool gvrStatus;
    float gvrTimer;
    public LevelChanger m_LevelChanger;
    public GameObject Teleporter;
    Camera cam;
...

        Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            if(imgGaze.fillAmount == 1 && hit.transform.CompareTag("TeleportZuVR"))
            {
                Debug.Log(hit.transform.name);
                Teleporter = hit.transform.gameObject;
                m_LevelChanger.FadeToScene(m_LevelChanger.sceneToLoad);
            }
            else if (imgGaze.fillAmount == 1 && hit.transform.CompareTag("TeleportZuAR"))
            {
                m_LevelChanger.FadeToScene(m_LevelChanger.sceneToLoad);
            }
        }
    }

In the following script the teleporter is selected with gauze and after 2 seconds the transition to the VR scene starts. In line 19, I save the name of the teleporter.

Now I want the camera to be set to a certain position in the AR scene, depending on which teleporter was selected.
Therefore I have a second script, which is attached to the VR-Camera.

public class CameraPositioning : MonoBehaviour
{
    public VRGaze m_VRGaze;
    public GameObject teleportZelt;

    // Update is called once per frame
    void Update()
    {
        if (m_VRGaze.Teleporter.name == "Teleport_Zelt")
        {
        transform.position = teleportZelt.transform.position;

        }
    }
}

During the running time in AR I see in the inspector that the right teleporter was selected. When the VR scene is loaded, the name disappears from the inspector and Unity displays the error message:
UnassignedReferenceException: The variable Teleporter of VRGaze has not been assigned.
You probably need to assign the Teleporter variable of the VRGaze script in the inspector.

If I drag the correct teleporter into the field in the inspector during runtime, the camera position is also changed. So that works. Only the assignment is incorrect.

Can you tell me where the error is?

When you change scenes, all objects in the scene are destroyed, and any references to them become effectively null.

To avoid this, you should store the name of the selected teleporter (not the GameObject reference) in a static variable. Static variables retain their value even without any objects around, so they can carry data across the scene change.

1 Like

Hi JoeStrout,
thank you! That’s exactly what helped me.

So i added in my first script:

public static string teleporter_Name;

and this in my Raycast

teleporter_Name = hit.transform.name;

and in my second script:

public GameObject teleporter;
  

    private void Start()
    {
        teleporter = GameObject.Find(VRGaze.teleporter_Name);
        Debug.Log(teleporter + " in VR");
    }
1 Like