[BUG] MissingReferenceException when I remove an object using the StateSystem

Hi,

I have a hair with physics using BonesStimulator and during "Fly" state I disable the physics, because it looks very weird, so this is my code, but I'm pretty sure its going to be replicable with any other script not just BonesStimulator.

C#:
    public class EnableBonesStimulatorStateBehavior : StateBehavior
    {
        [SerializeField] protected bool _isEnabled;

        [SerializeField] private BonesStimulator _bonesStimulator;
       
        public bool IsEnabled
        {
            get => _isEnabled;
            set
            {
                _isEnabled = value;
                _bonesStimulator.enabled = value;
            }
        }

    }

The problem comes when I destroy the hair (unequip it) and I use my fly ability, i get this error, pointing to Ability.cs line 577 "
StateManager.SetState(m_GameObject, m_State, active);
"

The Error

MissingReferenceException: The object of type 'BonesStimulator' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

I guess the state system keeps cache of all the objects, but now that the BonesStimulator (hair) is destroyed it fires this error because its null. How can I fix that?
 
Thanks for the detailed report. From what I can see, this appears to be related to custom code rather than the asset itself. Supporting custom implementations is outside the scope of free support, but if you’d like, you’re welcome to contact us via email to discuss paid support options.

That said, if you’re able to reproduce the exception in the demo scene of a fresh project, I’d be happy to take a closer look. If it does turn out to be an issue with our code, I will of course refund you.
 
Back
Top