Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Disable, Enable and Reset ArCore

Discussion in 'ARCore' started by danielesuppo, Jul 27, 2018.

  1. danielesuppo


    Oct 20, 2015
    I'm working on a geo-referenced app, to discover some AR placeholders in several locations.
    To preserve the battery I think to disable ARCore while the player is just walking to the new location (but the app must stay open because there's a map in it that show the placeholders positions).

    1) So to disable ARcore I use -- arcoreSession.enabled = false;

    2) Next. to enable it again I use -- arcoreSession.enabled = true;

    3) At the same time I have to reset AR camera position (to 0,0,0) and orientation, so I use -- LifecycleManager.Instance.ResetSession();

    My issue is that this is working only once, the 2nd time I get the error "Trying to access a session that has already been destroyed"

    Is there a way to safely reset the ARCore session, so to be able to repeat this workflow as many times as needed?

  2. danielesuppo


    Oct 20, 2015
    If someone is interested I've made this code and it seem to work as expected

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class ARCoreSessionManagement : MonoBehaviour {
    7.     [SerializeField] GameObject arCoreSessionPrefab;
    8.     private GameObject newArCoreSessionPrefab;
    9.     private GoogleARCore.ARCoreSession arcoreSession;
    12.     private void Start()
    13.     {
    14.         newArCoreSessionPrefab = Instantiate(arCoreSessionPrefab,, Quaternion.identity);
    15.         arcoreSession = newArCoreSessionPrefab.GetComponent<GoogleARCore.ARCoreSession>();
    16.         arcoreSession.enabled = true;
    17.     }
    21.     public void Reset()
    22.     {
    23.         StartCoroutine(CreateANewSession());
    24.     }
    26.     IEnumerator CreateANewSession()
    27.     {
    28.         //Destroy
    29.         arcoreSession.enabled = false;
    30.         if (newArCoreSessionPrefab != null)
    31.             Destroy(newArCoreSessionPrefab);
    33.         yield return new WaitForSeconds(1);
    35.         //Create a new one
    36.         newArCoreSessionPrefab = Instantiate(arCoreSessionPrefab,, Quaternion.identity);
    37.         arcoreSession = newArCoreSessionPrefab.GetComponent<GoogleARCore.ARCoreSession>();
    38.         arcoreSession.enabled = true;
    39.     }
    40. }