1 (edited by CELL9 2022-12-16 02:09:55)

Topic: Getting coordinates from other objects/markers

How do I get the coordinates of a marker/GameObject created by another script?
I have a script that should spawn a marker at another markers location. But the only way it works now is to set the object(clone) created under 3D Markers to the target in the script during runtime, and then run the script. It only spawns it in the correct location if the marker is in view (I think I know why, I just don't know how to do it correctly).

using System;
using UnityEngine;

public class SpawnAtObject : MonoBehaviour
    public GameObject prefab; // Spawned object prefab
    public GameObject target; // Target
    public float delay = 5f; // Spawn delay
    public float distance = 10f; // Spawn distance
    public float maxDistance = 10f; // Max random distance
    public float minDistance = 1f; // Min random distance
    public float rotation = 0; // Spawn direction from target
    public bool randomRotation;
    public bool randomDistance;
    private double targetLat;
    private double targetLng;
    private OnlineMaps map;
    private OnlineMapsMarker3D marker;

    private void Start()
        // Call the SpawnObject method after the specified delay
        Invoke("SpawnObject", delay);

    private void SpawnObject()
        if (randomDistance)
            distance = UnityEngine.Random.Range(minDistance, maxDistance);
        if (randomRotation)
            rotation = UnityEngine.Random.Range(1, 360);
        // Get the coordinates of the target
        Vector3 targetPosition = target.transform.position;
        OnlineMapsTileSetControl.instance.GetCoordsByWorldPosition(targetPosition, out targetLng, out targetLat);

        // Get the spawn distance from the target
        OnlineMapsUtils.GetCoordinateInDistance(targetLng, targetLat, distance, rotation, out targetLng, out targetLat);

        // Create the marker at the spawn location
        marker = OnlineMapsMarker3DManager.CreateItem(targetLng, targetLat, prefab);

Re: Getting coordinates from other objects/markers


Your problem has a lot of solutions, the correct one of which depends on the specific task, for example:
1. If this script is called only once, you can store the marker in a static field.
2. If this script is called many times:
a. You can store markers in a static list.
b. Use label (OnlineMapsMarkerBase.label), tags (OnlineMapsMarkerBase.tags) or customFields (OnlineMapsMarkerBase.customFields) to store some marker ID and then get marker from OnlineMapsMarker3DManager.

When you have a marker in another script, use OnlineMapsMarkerBase.GetPosition to get its coordinates.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.