Home > Software design >  Unable to deactivate game object on Unity MARS Body tracking lost
Unable to deactivate game object on Unity MARS Body tracking lost

Time:10-19

I have written a script to track and untrack human body using Unity MARS SDK here a sphere gameObject is made a child of body prreset, which provided by Unity MARS SDK. the sphere object becomes active successfully when body is tracked but it's not deactivated when body is lost. Below is the script.

using System;
using System.Collections.Generic;
using Unity.MARS.Data;
using Unity.MARS.Providers;
using Unity.XRTools.ModuleLoader;
using UnityEngine;
using UnityEngine.UI;

public class BodyTrackManager : MonoBehaviour, IUsesMarsBodyTracking, IUsesFunctionalityInjection
{
    IProvidesMarsBodyTracking IFunctionalitySubscriber<IProvidesMarsBodyTracking>.provider { get; set; }
    IProvidesFunctionalityInjection IFunctionalitySubscriber<IProvidesFunctionalityInjection>.provider { get; set; }

    [SerializeField] GameObject testObject;

    void Awake()
    {
        this.EnsureFunctionalityInjected();
    }

    private void OnEnable()
    {
        this.SubscribeBodyAdded(OnBodyAdded);
    }

    void OnDisable()
    {
        this.UnsubscribeBodyAdded(OnBodyRemoved);
    }
    void OnBodyAdded(IMarsBody body)
    {
        testObject.SetActive(true);
    }

    void OnBodyRemoved(IMarsBody body)
    {
        testObject.SetActive(false);
    }
}

This script is attached to empty gameobject in the scene.

CodePudding user response:

We talked about this in a DM on the forum, but for anyone else who comes across this issue, what you want looks something like this:

    void OnEnable()
    {
        this.SubscribeBodyAdded(OnBodyAdded);
        this.SubscribeBodyRemoved(OnBodyRemoved);
    }
    void OnDisable()
    {
        this.UnsubscribeBodyAdded(OnBodyAdded);
        this.UnsubscribeBodyRemoved(OnBodyRemoved);
    }

It sounds like this still isn't quite working (OnBodyRemoved not getting called) which might be a bug in MARS. We're looking into it.

  • Related