Game stops running, but completes the current method.

Hello Everybody!

So far I’ve been under the impression that if something breaks the code instantly stops working, but I am having a bit of trouble atm and am not getting an error message at all.

I will try to detail my problem a bit. I work with a grid-map (Hexes to be specific).

I have the following functions in my “Hex” Class:

    public List<Hex> GetAllNeighbours()
    {
        if(neighbourDictionary == null){
            FindNeighbours();
        }
        return neighbourDictionary.Values.ToList();
    }
    public void FindNeighbours()
    {       
        neighbourDictionary = new Dictionary<(int,int), Hex>();
        foreach (var direction in directions)
        {
            neighbourDictionary[direction] = hexMap.GetHexAtGridLocation(Q + direction.Item1, R + direction.Item2);
        }
    }

Directions is an array of tuples that determine the axial directions (using axial/cube corrdinates)

The later creates a dictionary of neighbours. Sometimes however a Hex won’t have 6 neighbours so some of the entries will be “null” instead. And I guess this is were the trouble comes from.

Now I have a function in a “HexMap” MonoBehaviour that calls GetNeighbours() and does something with the List of Hexes. It is currently called from “Update” with a button press. That function looks something like this:

public void highlight_targetable_Hexes(){
        targetableHexes = hexDictionary[(1,1)].GetAllNeighbours(); ///(1,1) is just a position on the hexMap
        foreach (var hex in targetableHexes)
        {
            if (hex != null)
            {
                     ///Replacement for code
                     Debug.Log("Working");
            }
        }
    }

If I do this on a hex-tile that has 6 neighbours it works perfectly fine and continues running afterward. However when I use this on a hex with less than 6 neighbours I run into trouble.

The thing that is most odd to me is, that the code does not immediately break when confronted with the first “null” value. In fact it prints the correct amount of “Working” messages and even Debug messages put at the end of the faulty method. However after completing the method, the game just completely stalls and breaks (animations stop, input doesn’t work, I guess the entire game breaks down).

Since no error message a is shown and due to the fact that I always though a faulty method would stop working immediatley I am kind of confused right now.

The obvious possibility is that the problem actually occurs later than you think, after all of the output that you see.

Game no longer responding sounds like it could be an infinite loop. Does the Unity editor also stop responding, or only the game?

1 Like

It’s just the game stops working. It’s just so odd this only breaks the game when it tries to get a hex without a full amount of 6 neighbours, so I kind of think the problem needs to be somewhere in here.

After the call of the test function nothing happens anymore in the Update method (nor in any other update method, since there is very little I actually update non-manually currently except for mouse position. In fact I am mostly using plain c# scripts.

Trying to pinpoint the problem I’ve skipped the step from “GetAllNeighbours” to instantly use "Findneighbours upon pressing the space key. It still breaks when the Hex in question has less than 6 neighbours. It is odd, because the GetHexAtGridLocation(q,r) used in it actually uses a try catch and returns “null” in the catch case. It is all very odd. If the problem were the dictionary being unable to handle the null entries, shouldn’t the code just immediately snap isntead of completing it?

Regarding the Infinite loop. Maybe, but I don’t think there is any involved/happening after this function.

edit: Also dude your amazing, I think you were in all the threads I posted so far!!!

Is it possible that the game is paused? (Check the play/stop/pause buttons near the top of the editor.) It’s possible to put the editor into a mode where it will automatically pause if any script writes to Debug.LogError.

Another thing that sometimes looks a lot like pausing is if Time.timeScale somehow got set to zero. (Though this won’t actually stop Update from running.)

1 Like

You are right… It is paused. I’m a dunce.