News Forums RAIN General Discussion and Troubleshooting Crash - Missing reference exception

This topic contains 5 replies, has 3 voices, and was last updated by  strangegames 7 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #39691

    strangegames
    Participant

    I got this crash in my game and was wondering if you could shed some light on what’s going on. The AI’s are attacking buildings with UFPS damage handler scripts. The damage handler will call UnityEngine.Object.Destroy() on the gameobject which has a visual aspect attached. Will destroying a gameobject with a RAIN entity attached possibly cause issues if the AI’s sensors are possibly accessing it via a different thread?

    Thanks,
    Reggie

    MissingReferenceException: The object of type ‘GameObject’ 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.
    RAIN.Perception.Sensors.PhysicalSensor.IsDetected (System.Object aGameObject, System.String aAspectName)
    RAIN.Perception.BasicSenses.IsDetected (RAIN.Perception.Sensors.RAINSensor aSensor, System.Object aObject, System.String aAspectName)
    RAIN.Perception.BasicSenses.IsDetected (System.Collections.Generic.List`1 aSensors, System.Object aObject, System.String aAspectName)
    RAIN.BehaviorTrees.Actions.DetectAction.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.Minds.BasicMind.Think ()
    RAIN.Core.AI.Think ()
    RAIN.Core.AIRig.AIUpdate ()
    RAIN.Core.AIRig.Update ()

    #39729

    tgraupmann
    Participant

    With uFPS you can have a custom damage handler. If you override the Die method you can turn off the AI before calling base.Die() to avoid any hiccups.

    public class EnemyHit : vp_DamageHandler
        {
            public override void Die()
            {
                AIRig aiRig = gameObject.GetComponentInChildren<AIRig>();
                if (aiRig)
                {
                    aiRig.enabled = false;
                }
                else
                {
                    Debug.LogError("No ai rig found!");
                }
                StartCoroutine(DestroyRoutine());
            }
            public IEnumerator DestroyRoutine()
            {
                yield return new WaitForSeconds(5f);
                base.Die();
            }
        }

    Although I deactivate the AIRig and wait, that’s only because I’m playing a death animation. You could call aiRig.AI.Stop() and let base.Die() destroy the gameObject.

    • This reply was modified 7 months ago by  tgraupmann.
    #39734

    strangegames
    Participant

    In this case, the AI are attacking a building and they actually don’t need to be deactivated. I think it’s the entity on the building that’s the problem.

    #39735

    tgraupmann
    Participant

    Oh your AI attack target is being destroyed. I guess you need a CustomAction to go through the attacking AI and if any variables reference the GameObject that will be destroyed, set it to null.

    #39739

    Sigil
    Keymaster

    This is also a bug in our detection code. Normally when an entity gets destroyed it removes itself from our SensorManager (and it still is, or should be) but we added a “consistent” option to our detect nodes, which attempts to keep the same detected aspect/object each frame, and it fails to realize that the object has been destroyed before sending it back into our sensor system.

    Similar to what @tgraupmann said, you could put in a custom action right before your detect node that checks the previously detected target and clears it if it is destroyed, before trying to detect again.

    It will be fixed in the next version of RAIN.

    #39743

    strangegames
    Participant

    I’ll just modify the scripts to set the object inactive instead of destroying it.

    Thanks,
    Reggie

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.