diff --git a/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/DepthBufferLiDAR/DepthBufferLiDARSensor.cs b/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/DepthBufferLiDAR/DepthBufferLiDARSensor.cs index 46092a91..3588394c 100644 --- a/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/DepthBufferLiDAR/DepthBufferLiDARSensor.cs +++ b/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/DepthBufferLiDAR/DepthBufferLiDARSensor.cs @@ -38,7 +38,17 @@ public class DepthBufferLiDARSensor : LiDARSensor protected override void Init() { - base.Init(); + if (scanPattern == null) + { + Debug.LogWarning("Initialization postponed: scanPattern is null. Ensure that scanPattern is assigned before calling Init."); + return; + } + Initialize(); + } + + public override void Initialize() + { + base.Initialize(); _transform = this.transform; SetupCamera(); LoadScanData(); diff --git a/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/LiDARSensor.cs b/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/LiDARSensor.cs index febec48a..db91a30b 100644 --- a/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/LiDARSensor.cs +++ b/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/LiDARSensor.cs @@ -34,6 +34,15 @@ public abstract class LiDARSensor : UnitySensor, IPointCloudInterface public int pointsNum { get => _pointsNumPerScan; } protected override void Init() + { + if (_scanPattern == null) + { + return; + } + Initialize(); + } + + public virtual void Initialize() { _pointsNumPerScan = Mathf.Clamp(_pointsNumPerScan, 1, scanPattern.size); _pointCloud = new PointCloud() diff --git a/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/RaycastLiDAR/RaycastLiDARSensor.cs b/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/RaycastLiDAR/RaycastLiDARSensor.cs index eee45f04..8c378f6b 100644 --- a/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/RaycastLiDAR/RaycastLiDARSensor.cs +++ b/Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/RaycastLiDAR/RaycastLiDARSensor.cs @@ -30,7 +30,17 @@ public class RaycastLiDARSensor : LiDARSensor protected override void Init() { - base.Init(); + if (scanPattern == null) + { + Debug.LogWarning("RaycastLiDARSensor: scanPattern is null. Initialization is delayed until scanPattern is set."); + return; + } + Initialize(); + } + + public override void Initialize() + { + base.Initialize(); _transform = this.transform; diff --git a/Assets/UnitySensorsROS/Runtime/Scripts/Publishers/SensorMsgs/PointCloud2Msg/LiDARPointCloud2MsgPublisher.cs b/Assets/UnitySensorsROS/Runtime/Scripts/Publishers/SensorMsgs/PointCloud2Msg/LiDARPointCloud2MsgPublisher.cs index 171836a7..7a3fd5c8 100644 --- a/Assets/UnitySensorsROS/Runtime/Scripts/Publishers/SensorMsgs/PointCloud2Msg/LiDARPointCloud2MsgPublisher.cs +++ b/Assets/UnitySensorsROS/Runtime/Scripts/Publishers/SensorMsgs/PointCloud2Msg/LiDARPointCloud2MsgPublisher.cs @@ -12,6 +12,11 @@ public class LiDARPointCloud2MsgPublisher : PointCloud2MsgPublisher private void Awake() { + if (_source == null) + { + Debug.LogError("Source is not set in LiDARPointCloud2MsgPublisher. Please ensure that the '_source' field is assigned in the Unity Editor or via code. Expected type: IPointCloudInterface."); + return; + } _serializer.SetSource(_source as IPointCloudInterface); } }