Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Reset AR Session

Discussion in 'Handheld AR' started by blanx, Jun 22, 2018.

  1. blanx

    blanx

    Joined:
    Feb 26, 2016
    Posts:
    57
    Is there a way to reset the AR session?

    I tried to use this in, but it seems it does not do anything:
    ARSubsystemManager.DestroySubsystems();
    ARSubsystemManager.CreateSubsystems();

    Sometimes the tracking goes wrong or when reopening an app all es messed up and I want to give the users to reset the scene. Then all planes have to removed and the tracking points and so on.
    For ARKit I had a script that can do this, but now with foundation it seems a bit more difficult.
     
  2. jimmya

    jimmya

    Unity Technologies

    Joined:
    Nov 15, 2016
    Posts:
    793
    You should be able to destroy the ARSession GameObject and Instantiate a new one to achieve this.
     
  3. blanx

    blanx

    Joined:
    Feb 26, 2016
    Posts:
    57
    Thanks. Will try that.
    And then it is just a matter of destroying the visual planes to have a clean new session?
     
  4. TijsVdV

    TijsVdV

    Joined:
    Mar 7, 2017
    Posts:
    15
    Did this work for you? I tried this but when but i get this error when enabeling the newly spawned arsession: InvalidOperationException: Cannot start AR session because there is no session subsystem
     
  5. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    232
    Same here, "InvalidOperationException: Cannot start AR session because there is no session subsystem." when enabling ARSession. I also get it if I load from SampleScene to another scene and then try to load SampleScene again.

    Update: Exception only occurs in the Editor.
     
    Last edited: Jun 27, 2018
  6. StefanAugmentors

    StefanAugmentors

    Joined:
    Nov 3, 2017
    Posts:
    15
    Hi. Did destroying the AR Session object and re-instantiating work for anyone? I get a black screen when I do that.
     
  7. WolfBeardedLion

    WolfBeardedLion

    Joined:
    Apr 5, 2013
    Posts:
    27
    I'm also getting a black screen, instead of the camera feed, after destroying the ARSession component and then adding it back to the same gameObject.
     
  8. StefanAugmentors

    StefanAugmentors

    Joined:
    Nov 3, 2017
    Posts:
    15
    I couldn't find a nice way to reset the session. For now I get all the planes and point clouds and destroy/disable them manually.
     
  9. Phoera

    Phoera

    Joined:
    Mar 10, 2015
    Posts:
    9
    If I'm reloading AR scene for the second time, I'm getting the "Cannot start AR session.." error, and device is not tracking any planes, due to session fail
     
  10. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    555
    I just did a test on a Pixel where I enabled/disabled the ARSession component, and also Destroyed and Instantiated it. All seemed to work as expected.

    For this particular issue:
    What device and OS version?
     
  11. Cowcak

    Cowcak

    Joined:
    Mar 11, 2015
    Posts:
    8
    I have the same issue when loading scene again. Just one note that I'm using multiple scenes loaded additively and AR session lives in the scene from scene load.
     
  12. Phoera

    Phoera

    Joined:
    Mar 10, 2015
    Posts:
    9
    I can't already reproduce this exact error, it's been lost in the iterations of fixing attempts.
    The thing is I was trying to fix nonexistent error, because it was only in the Editor, as soon as I built my app for device, everything went okay.
    I used directives for ARSubsystemManager functions, because all of the systems are null in the editor, therefore it can't function properly.

    So it looks like this and works like charm even when reloading scene:

    Code (CSharp):
    1. void Start()
    2.     {
    3. #if !UNITY_EDITOR
    4.         ARSubsystemManager.DestroySubsystems();
    5.         ARSubsystemManager.CreateSubsystems();
    6.         ARSubsystemManager.StopSubsystems();
    7.         arSession.gameObject.SetActive(false);
    8. #endif
    9.     }
    10.  
    11. public async void StartTracking()
    12.     {
    13. #if !UNITY_EDITOR
    14.         arSession.gameObject.SetActive(true);
    15.         ARSubsystemManager.StartSubsystems();
    16.         arPlaneManager.planeAdded += PlaneTracked;
    17. #else
    18.         //here comes some emulation of plane tracking for Editor:
    19.         //PlaceInfinitePlane(Vector3.zero);
    20.         //await new WaitForSeconds(1f);
    21.         //OnPlaneTracked?.Invoke();
    22. #endif
    23.     }
    24.  
    May it help someone to figure out how to handle reload of ar session =)
     
    Last edited: Jul 12, 2018
    WolfBeardedLion likes this.
  13. StefanAugmentors

    StefanAugmentors

    Joined:
    Nov 3, 2017
    Posts:
    15
    Same here. I think multiple scenes is breaking something.
     
  14. headlessstudio

    headlessstudio

    Joined:
    Feb 25, 2016
    Posts:
    76
    We're also having the same issue with reloading the scene a second time and getting a black screen. Samsung S8 and Android, and iPhone 7 Plus and iOS 12
     
  15. FlorentPoittevin

    FlorentPoittevin

    Joined:
    Jun 29, 2019
    Posts:
    2
    Same problem here
     
  16. FlorentPoittevin

    FlorentPoittevin

    Joined:
    Jun 29, 2019
    Posts:
    2
  17. azanoni

    azanoni

    Joined:
    Jul 30, 2019
    Posts:
    3
    But how we can call that method? I have a button that calls "ResetDemo" function to load the firstScene and reset the ARKit but doestn works :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using UnityEngine;
    5. using UnityEngine.XR.ARFoundation;
    6. using UnityEngine.XR.ARSubsystems;
    7. using UnityEditor;
    8. using UnityEngine.SceneManagement;
    9.  
    10. public class HumanBodyTracker : MonoBehaviour
    11. {
    12. ....
    13.     private void Reset() {
    14.          Debug.Log("Reseting....");
    15.     }
    16.  
    17.     public void ResetDemo()
    18.     {
    19.         Reset();
    20.         SceneManager.LoadScene("StartScene");
    21.         Debug.Log("Loading Start Scene...");
    22.         message = "Loading Start Scene...";
    23.  
    24.     }
     
  18. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24


    You have to get a reference to the ARSession in your scene to be able to call the Reset(). You can either get it by placing your script on the same GameObject, as the ARSession is on
    Code (CSharp):
    1. ARSession arSession = GetComponent<ARSession>();
    2. arSession.Reset();

    or you can link it by inserting a serialized field and set the reference in the inspector
    Code (CSharp):
    1. using UnityEngine.XR.ARFoundation;
    2.  
    3. [SerializeField]
    4. private ARSession _arSession;
    5.  
    6.  

    and call it in your method as follows
    Code (CSharp):
    1. _arSession.Reset();

    Unfortunately this Reset() doesn't solve the problem with the black screen in my project... So I raise my hand to say: "Same problem here";)
     
  19. azanoni

    azanoni

    Joined:
    Jul 30, 2019
    Posts:
    3
    Thanks, works like "its reseting" but same issue, black screen I think that maybe we need to store the gameobjects inside of the session...
     
  20. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    The Reset() works fine, if you don't leave the scene and stay in the AR-scene. As I can see from your posting you are then also loading another scene within your special reset, so this causes the black screen. Comment that out, so your app stays in the same scene and try the reset again.

    The problems stays the same: If you leave an AR-scene and later return to an AR-scene the background is just black :(
     
  21. vincentfretin

    vincentfretin

    Joined:
    Jul 4, 2019
    Posts:
    7
    Hi, I didn't play with multiple scenes yet, so I'm not sure if it's relevant, in chapter 20 of the "Unity AR & VR, by Tutorial" book, they have two scenes, and they add to the ARSession and ARSessionOrigin gameobjects a DontDestroyOnLoad script so those objects are not destroyed when switching scene.

    using UnityEngine;
    public class DontDestroyOnLoad : MonoBehabiour {
    void Awake()
    {
    DontDestroyOnLoad(gameObject);
    }
    }
     
  22. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    Thanks for your hint with the "DontDestroyOnLoad" script. I just tried your solution, but sadly that didn't fix the problem. I then also placed the script on the AR-camera (where the background and manager scripts are placed on), but that also didn't fix it and I'm getting still the black screen after loading a AR-scene more than once :(
     
  23. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    Ok, the black screen after scene-change is more easier to fix, than I thought! I created a small example project to commit a bugreport and while creating that project I discovered that I was just about to install the preview package of ARFoundation, again. As a software-developer one should be aware that preview versions might not be that 100% tested and debugged... so I went with the latest version without "preview" and the problem was solved. So just install the packages that are NOT in preview and the black screen is gone. Only fools like me install preview packages and wonder, why they aren't perfectly working :rolleyes: So sorry for all confusions from my side to this topic.

    The project now works with the followig packages:
    • ARFoundation 2.1.3
    • ARCore 2.1.1
    • ARKit 2.1.1
    I hope this might help somebody else :)
     
  24. LuisCarlosContreras

    LuisCarlosContreras

    Joined:
    Aug 7, 2018
    Posts:
    3
    Welp, I did set those packages and the problem persist... still black camera, I see is a common issue on forums
     
  25. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    Hmm, for my project this helps to fix the black screen problem after scene-change and didn't introduce it at reset. Also my sample-project does the Reset() and scene-change without the black screen multiple times.
    You are right, many people seem to report this issue, but the progression of ARFoundation is quite quick and maybe also the problems of these people are already fixed.
    Do you do some extra tasks in your reset-method that might cause problems? Are there any errors in the debug-output in the logcat?
     
  26. THDevelopment

    THDevelopment

    Joined:
    Jun 17, 2014
    Posts:
    2
    Hello there!

    I have a problem, when resetting the ARSession. I get a "rainbow disorder" which maybe has something to do with the camera texture, because you cant see it, if there is some GUI above.

    Do you guys may know how to avoid this? I attached one image, so you can get a better idea about the problem.

    It always appears when calling arSession.Reset(), or sometimes when reopen the Application and you´re in the active Session Screen.

    Every suggestion would be Great - Thank you very much!
     

    Attached Files:

  27. joepeijkemans

    joepeijkemans

    Joined:
    Apr 24, 2018
    Posts:
    1
    Are there any fixes yet? I have the same bug. It's not a big problem as everything works fine again after a second or so, but I have a loading screen in my app as well, so I might as well place that one there instead if possible.
     
  28. DeloitteCam

    DeloitteCam

    Joined:
    Jan 23, 2018
    Posts:
    11
  29. AAK_Lebanon

    AAK_Lebanon

    Joined:
    May 30, 2015
    Posts:
    69
    You can still hide it in a derived class and do whatever you want to do. note that they marked it as public, so you can call it from your script...
     
unityunity