Hi there.
I can’t figure out a thing reguarging unity events.
Basically, i have a script with some methods, in two of these methods i call an event to executing a method of another script.
here’s the code:
Here’s where i initiate the event:
public UnityEvent<Pose, TrackingState, int> OnAnchorPoseUpdate;
Here’s one method that calls the event:
public void SaveData()
{
if (data == null || path == "")
return;
Debug.Log("data value: " + data.Count);
string json = JsonHelper.ToJson<AnchorData>(data);
if (json == null)
return;
Debug.Log("Json value: " + json);
File.WriteAllText(path, json);
Debug.Log("Saved in " + path);
OnDebugSent.Invoke(string.Format("Sending last added anchor: UUID {0}, UDK {1}, worlAnchor UUID {2}, worldAnchor UDK{3}", lastAddedAnchor.UUID, lastAddedAnchor.UserDefinedKey, lastAddedAnchor.worldAnchor.UUID, lastAddedAnchor.worldAnchor.UserDefinedKey));
CallPoseUpdate(lastAddedAnchor.worldAnchor, lastAddedAnchor.markerID);
}
And finally the other method that calls the event:
public IEnumerator LoadAnchor(int markerID, AnchorData anchor)
{
if (anchor == null)
yield break;
Debug.Log("loading anchor");
localMap.Load(anchor.UUID, anchor.UserDefinedKey);
#if !UNITY_EDITOR
int counter = 0;
do
{
OnDebugSent.Invoke("Trying to get a tracking nrWorldancor: " + counter++);
yield return new WaitForSeconds(0.6f);
anchor.worldAnchor = GetWorldAnchor(anchor.UUID);
} while (anchor.worldAnchor.CurrentTrackingState != TrackingState.Tracking && counter <= 5);
#elif UNITY_EDITOR
yield return new WaitForSeconds(0.4f);
anchor.worldAnchor = GetWorldAnchor(anchor.UUID);
#endif
anchor.worldAnchor.gameObject.name = "NRAnchor_" + markerID;
loadedAnchors.Add(anchor.markerID);
CallPoseUpdate(anchor.worldAnchor, markerID);
}
The CallPoseUpdate method is the following:
if(null == anchor)
{
OnDebugSent.Invoke("anchor is null");
return;
}
if(null == anchor.transform)
{
Debug.Log("transform is null");
return;
}
Pose newPose = new Pose(anchor.transform.position, anchor.transform.rotation);
if(newPose == null)
{
Debug.Log("new pose is null");
}
OnDebugSent.Invoke(string.Format("Calling poseUpdate with thees values: newPose {0}, ancor.CurrentTrackingState {1}", newPose, anchor.CurrentTrackingState));
OnAnchorPoseUpdate.Invoke(newPose, anchor.CurrentTrackingState, markerID);
the fisrt method, SaveData(), gets called by another script, the second, LoadAnchor(int, AnchorData), is called by another method in the same script.
Here comes the part that confuses me: for some reason, when i try calling “CallPoseUpdate” from the SaveData() method, it won’t work, i even checked if some paramethers were null in debug mode but everything had a value, the only thing missing was the listener for some reason.
However, when called by LoadAnchor() method, then it will work.
I don’t know why, could you please explain?
Ask me whatever you didn’t understand. Thanks!!