Help Needed with AR Project in Unity: Object Position Fluctuates on Scene Reload

Hey everyone! :wave:

I’m working on an AR project in Unity (using AR Foundation) where I spawn objects representing city names based on my real-world location and orientation relative to each city. For example, if New York is to my left, an AR object saying “New York is here” should appear in the exact direction of New York.

The setup works correctly when I first launch the app—the objects spawn accurately relative to my position. However, when I reload the scene, the objects start to appear in random directions (e.g., New York shows up to my right instead of the left). I’m already restarting the compass and location services each time I destroy and respawn the city objects, but it hasn’t solved the problem.

I checked the location(my and target) is being fetched correctly. Also I noticed if an object is spawned in the enviroment then if any car or person moved in a camera vision then the object also starts moving with it(car or person), this shows the ar lost the tracking at that state. The thing is same setup is working absolute fine while the app is fresh launch but fails when I want to get other city data(destroying previous city object from ar scene).

My target platform is android and ios for now.

Does anyone have experience with stabilizing object positioning on scene reloads in AR? Any tips on how to keep consistent object placements without directional fluctuations?

Thanks for any insights! :blush:

The Problem:

  • Initial Launch: City objects spawn accurately in their respective directions (e.g., New York appears on the left side if it’s to the user’s left).
  • After Reloading the Scene: The positions of the spawned objects fluctuate randomly. For instance, an object that should appear on the left might suddenly spawn on the right or another random direction.

The purpose of reloading a scene is that let’s say initially I checked where is New York then I want to check where is London.

Fixed this issue by adding below code and calling it each time before city object spawned. The accuracy after this is 9/10.

private IEnumerator __InitARStuff()
{
    yield return new WaitForSeconds(1f);

    Input.compass.enabled = false;

    // Stop the location service if it’s running
    if (Input.location.isEnabledByUser && Input.location.status == LocationServiceStatus.Running)
    {
        Input.location.Stop();
    }
    var xrManagerSettings = UnityEngine.XR.Management.XRGeneralSettings.Instance.Manager;
    xrManagerSettings.DeinitializeLoader();

    yield return new WaitForSeconds(1f);

    xrManagerSettings.InitializeLoaderSync();

    Input.compass.enabled = true;
    Input.location.Start();

    yield return new WaitForSeconds(1f);

}