From be36300c3735dd832751e1aeef858e2bc1743fe4 Mon Sep 17 00:00:00 2001 From: Mateusz Date: Wed, 30 Jun 2021 20:52:52 +0200 Subject: [PATCH] Bug fix --- MoreHazards/MoreHazards/Doors.cs | 16 ++++++++++++---- MoreHazards/MoreHazards/utils.cs | 3 +++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/MoreHazards/MoreHazards/Doors.cs b/MoreHazards/MoreHazards/Doors.cs index 53dcbb8..0c02ac5 100644 --- a/MoreHazards/MoreHazards/Doors.cs +++ b/MoreHazards/MoreHazards/Doors.cs @@ -85,10 +85,18 @@ public IEnumerator RandomDoorMalfunction() continue; //random door of the room the player is inside - var door = CollectionUtils.GetRandomElement((Map.FindParentRoom(player.GameObject).Doors)); - - door.NetworkTargetState = false; - Log.Debug("Door closed on player:"+player.Nickname); + try + { + var door = CollectionUtils.GetRandomElement((Map.FindParentRoom(player.GameObject).Doors)); + + door.NetworkTargetState = false; + Log.Debug("Door closed on player:" + player.Nickname); + } + catch (Exception e) + { + Log.Debug(e + " No doors found", MoreHazards.Instance.Config.Debug); + throw; + } } } } diff --git a/MoreHazards/MoreHazards/utils.cs b/MoreHazards/MoreHazards/utils.cs index e808117..03f3413 100644 --- a/MoreHazards/MoreHazards/utils.cs +++ b/MoreHazards/MoreHazards/utils.cs @@ -111,6 +111,9 @@ public static TValue GetRandomElement(List collection) public static TValue GetRandomElement(IEnumerable enumerable) { var collection = (enumerable as IReadOnlyList) ?? enumerable.ToArray(); + if (collection.Count <= 0) + throw new Exception("Collection is empty"); + int index = UnityEngine.Random.Range(0, collection.Count - 1); return collection[index]; }