From 08a66c4dc33340f4294643654bbe1dae74ca8ce7 Mon Sep 17 00:00:00 2001 From: Ruo-Ping Dong Date: Wed, 14 Apr 2021 16:45:15 -0700 Subject: [PATCH] Fix errors when creating new RayCast sensor (#5261) * check if tag and angle is null * add test * changelog --- com.unity.ml-agents/CHANGELOG.md | 1 + .../Runtime/Sensors/RayPerceptionSensor.cs | 2 +- .../Runtime/Sensor/RayPerceptionSensorTests.cs | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/com.unity.ml-agents/CHANGELOG.md b/com.unity.ml-agents/CHANGELOG.md index 9889188083..f2f8956dfb 100755 --- a/com.unity.ml-agents/CHANGELOG.md +++ b/com.unity.ml-agents/CHANGELOG.md @@ -66,6 +66,7 @@ This results in much less memory being allocated during inference with `CameraSe settings. Unfortunately, this may require retraining models if it changes the resulting order of the sensors or actuators on your system. (#5194) - Removed additional memory allocations that were occurring due to assert messages and iterating of DemonstrationRecorders. (#5246) +- Fixed a bug where agent trying to access unintialized fields when creating a new RayPerceptionSensorComponent on an agent. (#5261) ## [1.9.1-preview] - 2021-04-13 ### Major Changes diff --git a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs index 4380232cb2..4cb67ec709 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs @@ -78,7 +78,7 @@ public struct RayPerceptionInput /// public int OutputSize() { - return (DetectableTags.Count + 2) * Angles.Count; + return ((DetectableTags?.Count ?? 0) + 2) * (Angles?.Count ?? 0); } /// diff --git a/com.unity.ml-agents/Tests/Runtime/Sensor/RayPerceptionSensorTests.cs b/com.unity.ml-agents/Tests/Runtime/Sensor/RayPerceptionSensorTests.cs index 2145d98712..a4c7badc59 100644 --- a/com.unity.ml-agents/Tests/Runtime/Sensor/RayPerceptionSensorTests.cs +++ b/com.unity.ml-agents/Tests/Runtime/Sensor/RayPerceptionSensorTests.cs @@ -420,6 +420,19 @@ public void TestStaticPerceiveNoTags() Assert.AreEqual(-1, castOutput.RayOutputs[0].HitTagIndex); } } + + [Test] + public void TestCreateDefault() + { + SetupScene(); + var obj = new GameObject("agent"); + var perception = obj.AddComponent(); + + Assert.DoesNotThrow(() => + { + perception.CreateSensors(); + }); + } #endif } }