News › Forums › RAIN › General Discussion and Troubleshooting › Crash - Missing reference exception
Tagged: bug, destroyed gameobject, detect
This topic contains 5 replies, has 3 voices, and was last updated by strangegames 7 months ago.
-
AuthorPosts
-
December 12, 2022 at 2:54 pm #39691
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,
ReggieMissingReferenceException: 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 ()December 14, 2022 at 9:36 am #39729With 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.
December 14, 2022 at 9:59 am #39734In 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.
December 14, 2022 at 10:02 am #39735Oh 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.
December 14, 2022 at 11:53 am #39739This 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.
December 14, 2022 at 12:32 pm #39743I’ll just modify the scripts to set the object inactive instead of destroying it.
Thanks,
Reggie -
AuthorPosts
You must be logged in to reply to this topic.