Dictionary being cleared when reaching end of Update.

Hello everyone, I have an update function below that is supposed to be tracking bodies from the Kinect.

However I have an issue where the _Bodies dictionary doesn’t store anything and is always cleared when the Update loop starts back from the top. Once it gets to this point:
List knownIds = new List (_Bodies.Keys);

_Bodies is already at a count of zero. So the code that is supposed to remove a key value pair if it’s not being tracked by the Kinect anymore doesnt run. Also this means that it just keeps instantiating new objects over and over again. Can anyone see why my Dictionary might not be storing values properly?

private Dictionary<ulong, GameObject> _Bodies = new Dictionary<ulong, GameObject> ();
    
    void Update()      	
    {
                		if (BodySourceManager == null) {
                			return;
                		}
                        
                		_BodyManager = BodySourceManager.GetComponent<BodySourceManager> ();
                		if (_BodyManager == null) {
                			return;
                		}
                        
                		Kinect.Body[] data = _BodyManager.GetData ();
                		if (data == null) {
                			return;
                		}
                        
                		List<ulong> trackedIds = new List<ulong> ();
                		foreach (var body in data) {
                			if (body == null) {
                				continue;
                			}
                                
                			if (body.IsTracked) {
                				trackedIds.Add (body.TrackingId);
                			}
                		}

                        	List<ulong> knownIds = new List<ulong> (_Bodies.Keys);

                		// First delete untracked bodies
                		foreach (ulong trackingId in knownIds) {
                			if (!trackedIds.Contains (trackingId)) {
                				Destroy (_Bodies [trackingId]);
                				_Bodies.Remove (trackingId);
                			}
                		}
                
                		foreach (var body in data) {
                			if (body == null) {
                				continue;
                			}
                            
                			if (body.IsTracked) 
                			{   
                				if (!_Bodies.ContainsKey (body.TrackingId)) {
                					_Bodies [body.TrackingId] = InstantiateMyObject(body);
                				}
                
                				RefreshBodyObject (body);
                			}
                		}
                	}

Everytime you use ‘new’ you are overwriting what was there before. If you want the data to persist this will not work. Maybe this is your intention? I don’t know.

Also, you need to make sure your null check is working on _Bodies. Put a Debug.Log before the return to see what the situation is.

Remember that you are using an external Hardware sensor which isn’t managed by Unity, so my bet is that the Kinect is likely updating at a much slower rate than unity. in the frames that things are getting deleted is likely due to the Kinect is still generating the body for its next update (which is asynchronous to Unity’s Update). I’ve done a quick seach and I’ve seen that the kinect (not the unity wrapper, but the SDK itself) doesn’t present a body data on every frame (30 or 60 fps), but only on the frames that it is ready (usually much lower some examples I’ve seen are at 16fps).

what this likely means is that you shouldn’t be adding/removing Gameobjects based on tracked bodies inside an Update() call. instead try having the Gameobject always instantiated, but either hidden or moved off screen.

Or only go by the last updated frame. you likely want to get the last updated frame like is done in this guy’s body manager