// Update is called once per frame void Update() { int state = 0; if (BodyManager == null) { return; } _BodyManager = BodyManager.GetComponent <BodyManager>(); if (_BodyManager == null) { return; } Body[] data = _BodyManager.GetData(); if (data == null) { return; } // get the amount of bodies in the list of tracked bodies, then delete unknown bodies 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); } } // body oject foreach (var body in data) { if (body == null) { continue; } if (body.IsTracked) { if (!_Bodies.ContainsKey(body.TrackingId)) { _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId); } RefreshBodyObject(body, _Bodies[body.TrackingId]); } } }