Search Unity

Other Unity Editor Freezes when Testing my Game

Discussion in 'Scripting' started by Eddyan3D, Jan 9, 2023.

  1. Eddyan3D

    Eddyan3D

    Joined:
    Jan 9, 2023
    Posts:
    1
    Introduction:
    Hello, first of all, I apologize for my English.

    I am developing in unity 2d the snake game as a hobby, and I got a problem, I don't know if it's from my code, from unity, or from my pc.

    I searched on the official unity forum and others and found similar problems, but not my specific one.
    I modified my code based on some suspicions I had, but nothing manages to fix the problem.

    The Problem:
    As everyone knows, the snake has to eat the foods that are going to appear in the game to grow, and it is precisely at the moment of eating that the entire editor freezes completely, it won't let me close them, or minimize them, and it doesn't warn me that "unity isn't responding".
    This also happens if I build the game and test it.
    The only way to close them when this happens is from the task manager.
    But this only happens sometimes...Not every time... And that's why I don't know why this problem occurs.
    It can occur the first time, like the fifth, like the fourteenth...

    The Code:
    This is my GameController Script https://github.com/ElandY99/ArSnakeUnity/blob/main/GameController.cs
    This is my FoodController Script https://github.com/ElandY99/ArSnakeUnity/blob/main/FoodController.cs
    This is my NormalFood Script https://github.com/ElandY99/ArSnakeUnity/blob/main/NormalFood.cs

    (I put the github links to not take up so much space)

    The explanation:
    In the NormalFood Script:

    - Detect from the food when the player collides with it.
    - I send the food points to the FoodController Script.
    - I call the function grow() in the SnakeController.
    - Destroy() the NormalFood GameObject.
    - Call the createFood() function in the FoodContrllor Script.

    In the FoodController Script:
    - createFood() //get all the filled boxes of the board to substract them from the chances of appearance of other foods.
    - Instantiate the NormalFood prefab.
    - I assign a random position to the new food taking into account the spaces already occupied by the snake or obstacles on the board.

    In the GameController Script:
    - I declare a empty list of Vector2.
    - I add the position of the snake's head to the list.
    - I perform several loops adding all the other elements that currently occupy a space on the board. (body parts, obstacles, tunnels and other foods).

    Ending:

    I suspect that at some point it may enter an infinite loop, but I'm not sure, if this were the case, I don't think the editor would freeze completely.
    The only way to close them when this happens is from the task manager.

    I also suspect that some error may be generated by removing the GameObject of the food, and calling the getFilledBoxes() function.

    Just in case, I leave the specifications of my pc, in case that could be the problem.

    Processor: amd r5 2600
    Gpu: amd rx580 8gb
    Ram: 16gb 3600mhz

    If someone could guide me minimally it would be of great help. Thank You.
     
    Last edited: Jan 9, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You probably have an infinite loop somewhere.

    EDIT: yep, food controller, line 44. You can't do that. :)

    Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

    Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

    Absolutely NOTHING will happen... until your code either:

    - returns from whatever function it is running

    - yields from whatever coroutine it is running

    As long as your code is looping, Unity isn't going to do even a single frame of change. Nothing.

    No exceptions.

    "Yield early, yield often, yield like your game depends on it... it does!" - Kurt Dekker