diff --git a/Project/Assets/ML-Agents/Examples/Match3/Scripts/Match3Drawer.cs b/Project/Assets/ML-Agents/Examples/Match3/Scripts/Match3Drawer.cs index 539bc975cf..4442926724 100644 --- a/Project/Assets/ML-Agents/Examples/Match3/Scripts/Match3Drawer.cs +++ b/Project/Assets/ML-Agents/Examples/Match3/Scripts/Match3Drawer.cs @@ -105,6 +105,10 @@ void OnDrawGizmos() if (!m_Board) { m_Board = GetComponent(); + if (m_Board == null) + { + return; + } } var currentSize = m_Board.GetCurrentBoardSize(); diff --git a/com.unity.ml-agents/Editor/Match3ActuatorComponentEditor.cs b/com.unity.ml-agents/Editor/Match3ActuatorComponentEditor.cs index 0ae31172dd..0b072c5b52 100644 --- a/com.unity.ml-agents/Editor/Match3ActuatorComponentEditor.cs +++ b/com.unity.ml-agents/Editor/Match3ActuatorComponentEditor.cs @@ -11,6 +11,14 @@ public override void OnInspectorGUI() var so = serializedObject; so.Update(); + var component = (Match3ActuatorComponent)target; + var board = component.GetComponent(); + if (board == null) + { + EditorGUILayout.HelpBox("You must provide an implementation of an AbstractBoard.", MessageType.Warning); + return; + } + // Drawing the RenderTextureComponent EditorGUI.BeginChangeCheck(); diff --git a/com.unity.ml-agents/Editor/Match3SensorComponentEditor.cs b/com.unity.ml-agents/Editor/Match3SensorComponentEditor.cs index a8673e09d9..857bedeee8 100644 --- a/com.unity.ml-agents/Editor/Match3SensorComponentEditor.cs +++ b/com.unity.ml-agents/Editor/Match3SensorComponentEditor.cs @@ -11,6 +11,14 @@ public override void OnInspectorGUI() var so = serializedObject; so.Update(); + var component = (Match3SensorComponent)target; + var board = component.GetComponent(); + if (board == null) + { + EditorGUILayout.HelpBox("You must provide an implementation of an AbstractBoard.", MessageType.Warning); + return; + } + // Drawing the RenderTextureComponent EditorGUI.BeginChangeCheck(); diff --git a/com.unity.ml-agents/Runtime/Integrations/Match3/Match3ActuatorComponent.cs b/com.unity.ml-agents/Runtime/Integrations/Match3/Match3ActuatorComponent.cs index e7b35dca9a..8dfa7e496e 100644 --- a/com.unity.ml-agents/Runtime/Integrations/Match3/Match3ActuatorComponent.cs +++ b/com.unity.ml-agents/Runtime/Integrations/Match3/Match3ActuatorComponent.cs @@ -1,3 +1,4 @@ +using System; using Unity.MLAgents.Actuators; using UnityEngine; using UnityEngine.Serialization; @@ -52,6 +53,11 @@ public bool ForceHeuristic public override IActuator[] CreateActuators() { var board = GetComponent(); + if (!board) + { + return Array.Empty(); + } + var seed = m_RandomSeed == -1 ? gameObject.GetInstanceID() : m_RandomSeed + 1; return new IActuator[] { new Match3Actuator(board, m_ForceHeuristic, seed, m_ActuatorName) }; } diff --git a/com.unity.ml-agents/Runtime/Integrations/Match3/Match3SensorComponent.cs b/com.unity.ml-agents/Runtime/Integrations/Match3/Match3SensorComponent.cs index f1b455a928..e9a1497e07 100644 --- a/com.unity.ml-agents/Runtime/Integrations/Match3/Match3SensorComponent.cs +++ b/com.unity.ml-agents/Runtime/Integrations/Match3/Match3SensorComponent.cs @@ -45,6 +45,10 @@ public override ISensor[] CreateSensors() Dispose(); var board = GetComponent(); + if (!board) + { + return Array.Empty(); + } var cellSensor = Match3Sensor.CellTypeSensor(board, m_ObservationType, m_SensorName + " (cells)"); // This can be null if numSpecialTypes is 0 var specialSensor = Match3Sensor.SpecialTypeSensor(board, m_ObservationType, m_SensorName + " (special)"); diff --git a/com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3ActuatorTests.cs b/com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3ActuatorTests.cs index dc3a7620f5..2beea36b32 100644 --- a/com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3ActuatorTests.cs +++ b/com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3ActuatorTests.cs @@ -194,5 +194,14 @@ public void TestMasking(bool fullBoard) // And they should add up to all the potential moves Assert.AreEqual(validIndices.Count + masks.HashSets[0].Count, board.NumMoves()); } + + [Test] + public void TestNoBoardReturnsEmptyActuators() + { + var gameObj = new GameObject("board"); + var actuatorComponent = gameObj.AddComponent(); + var actuators = actuatorComponent.CreateActuators(); + Assert.AreEqual(0, actuators.Length); + } } } diff --git a/com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3SensorTests.cs b/com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3SensorTests.cs index 26c5416cfa..a376bd3523 100644 --- a/com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3SensorTests.cs +++ b/com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3SensorTests.cs @@ -395,5 +395,14 @@ byte[] LoadPNGs(string pathPrefix, int numExpected) return bytesOut.ToArray(); } + + [Test] + public void TestNoBoardReturnsEmptySensors() + { + var gameObj = new GameObject("board"); + var sensorComponent = gameObj.AddComponent(); + var sensors = sensorComponent.CreateSensors(); + Assert.AreEqual(0, sensors.Length); + } } }