// Update is called once per frame void Update () { int state = 0; //If we don't have a body manager Game Object if( bodyManagerGO == null ) { //Exit the function return; } //Try to get the body manager game object's BodyManager component bodyManager = bodyManagerGO.GetComponent<BodyManager>(); //If the body manager game object doesn't have a BodyManager component if (bodyManager == null) { //Exit the function return; } //Create a local copy of the bodyManager data Body[] data = bodyManager.getData(); //If that data doesn't exist if( data == null ) { //Exit the function return; } //List of tracked body ID numbers List<ulong> trackedIDs = new List<ulong>(); //Fill that list //For each body in Kinect FOV foreach ( var body in data ) { //If the body data is empty if( body == null ) { //Skip this entry continue; } //If the body is being tracked if( body.IsTracked ) { //Add its ID to the trackedIDs list trackedIDs.Add(body.TrackingId); } } //Get list of all body gameobjec List<ulong> knownIDs = new List<ulong>(_Bodies.Keys); //Delete GameObjects for bodies that aren't being tracked foreach ( ulong trackingID in knownIDs ) { //If the currently tracked body ID list doesn't contain a body with this tracking ID if( !trackedIDs.Contains(trackingID) ) { //Delete the GameObject Destroy(_Bodies[trackingID]); //Remove the body from the list of gameObjects _Bodies.Remove(trackingID); } } //Update body objects that need to be updated and create body objects that need to be created foreach ( var body in data ) { //If the body doesn't exist if( body == null ) { //Skip it continue; } //If the body is tracked if( body.IsTracked ) { //If the body GameObject dictionary doesn't contain the body's tracking ID if( !_Bodies.ContainsKey(body.TrackingId) ) { //Create a body object with thr body's tracking ID and add it to the body GameObject dictionary (using the tracking ID as a key) _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId); } //Refresh the body object RefreshBodyObject(body, _Bodies[body.TrackingId]); } } }