Hi, how can I add object to some place in map using unity code?

I read almost every tutorial, but nothing is working. I can easily drag and drop it in unity with zero code, and using AR foundation and AR code i can see it in AR, but I want to do it through code, so I can spawn them easier. I tried everything, like this:

            var objAnchor = ARAnchorManagerExtensions.AddAnchor(aRAnchorManager, earthPosition.Latitude, earthPosition.Longitude, earthPosition.Altitude, Quaternion.identity);
            Instantiate(GeospatialAssetPrefab, objAnchor.transform);

(this code is returning null, and i dont know why)

or same thing but ARAnchorManager instead ARAnchorManagerExtensions, but none of this methods work, nothing is spawned.
I can do it like GameObject obj = Instantiate(GeospatialAssetPrefab, new Vector3(0, 0, -1), Quaternion.identity); but that new vector is something with no connection to world, it is basically unity coordinates, and i want to insert lat, long and altitude. Can someone please help me <3

Hi,

I don’t think our team is familiar with those AR-related classes. Can you give more information so we can help?

  • What does your scene hierarchy look like?
  • For the object that gets anchored – when you drag and drop it without code, what is it’s transform? Then, what is the transform when it spawns via code?

My hunch is that you’re not converting the latitude / longitude / altitude into the correct Unity coordinates to anchor the object. You can do this using CesiumWgs84Ellipsoid.LongitudeLatitudeHeightToEarthCenteredEarthFixed and CesiumGeoreference.TransformEarthCenteredEarthFixedPositionToUnity .