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);
}
}