Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Setting garbage collector mode is not supported on this platform

Discussion in 'Web' started by MiguelAppStudio, Sep 25, 2022.

  1. MiguelAppStudio

    MiguelAppStudio

    Joined:
    Nov 17, 2020
    Posts:
    19
    EDIT the question
    I create a small project and in that I have imported a third party project.
    Everything works perfectly in the Unity editor.
    The game is very simple and it is only necessary to touch a button for two balls to start moving.
    The problem arose when I created a build for (Build And Run), as the game starts perfectly but when I touch the mouse button to start moving the balls, it doesn't work.
    The button does not work and checking the Google console shows me the following error every time I touch the button to start.

    project.framework.js.gz:3 InvalidOperationException: Garbage collection mode setting is not supported on this platform

    I've never seen this, I've searched this community for other similar issues, but can't find any
    The button works perfectly in the Unity editor and on mobile devices, only this button doesn't work in WebGL, when we run the game in the browser.
    Thanks to @CodeSmile answer, I've read more documentation about Garbage Collector.
    In the project there is a file that I show below and it is the cause of the button not working correctly in WebGL.
    Delete said file and its references in other files, and everything works fine on PC, Devices, and WebGL.
    As I said, I have read the documentation about Unity's Garbage Collector but I still don't understand what it does.
    The game uses Garbage Collector in various parts, see:

    Code (CSharp):
    1. public void PauseGame()
    2.     {
    3.         GarbageCollectionManager.CollectGarbage();
    4.         PausePanel.SetActive(true);
    5.         highScoreTextPause.text = "High Score : " + GamePreferences.HighScore.ToString();
    6.         Time.timeScale = 0f;
    7.     }

    Code (CSharp):
    1. public void ResumeGame()
    2.     {
    3.         GarbageCollectionManager.CollectGarbage();
    4.         PausePanel.SetActive(false);
    5.         StartCoroutine(WaitToResume());
    6.     }

    Code (CSharp):
    1. public void GameOver()
    2.     {
    3.         GarbageCollectionManager.CollectGarbage();
    4.         TheGlobals.playingMode = false;
    5.     }
    Can you give me an idea why this happens?
    Where is there information on this?
    Could removing this Garbage Collector reference hurt my project in the long run?
    Thank you


    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.Scripting;
    5.  
    6. public static class GarbageCollectionManager
    7. {
    8.     public static void DisableGC()
    9.     {
    10.         #if !UNITY_EDITOR
    11.         GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
    12.         #endif
    13.     }
    14.  
    15.     public static void CollectGarbage()
    16.     {
    17.         #if !UNITY_EDITOR
    18.         GarbageCollector.GCMode = GarbageCollector.Mode.Enabled;
    19.         GC.Collect();
    20.         GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
    21.         #endif
    22.     }
    23. }
    24.  
     
    Last edited: Sep 26, 2022
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,583
    MiguelAppStudio likes this.
  3. MiguelAppStudio

    MiguelAppStudio

    Joined:
    Nov 17, 2020
    Posts:
    19
    Thanks for your reply.
    I must say that GameCircle is just the name of the Game.
    Regarding GarbageCollector .GCMode, I have gone through the documentation several times, but. I don't understand how this can affect my game, so I don't know what I should do.
    In none of the projects that I saw before I found this problem.
    The project actually has the following file, which is referenced somewhere in the application like so:
    Code (CSharp):
    1. GarbageCollectionManager.CollectGarbage();
    The file that creates this is the one I added to my question:
    Do you know why this happens and what function Garbage Collection has?
    I've read the Unity documentation over and over again, but I don't understand what exactly does this.
    I must say that if I remove this file and all its references in the application, everything works perfectly
    I don't understand how this function works

    Thanks for spend your time on me

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.Scripting;
    5.  
    6. public static class GarbageCollectionManager
    7. {
    8.     public static void DisableGC()
    9.     {
    10.         #if !UNITY_EDITOR
    11.         GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
    12.         #endif
    13.     }
    14.  
    15.     public static void CollectGarbage()
    16.     {
    17.         #if !UNITY_EDITOR
    18.         GarbageCollector.GCMode = GarbageCollector.Mode.Enabled;
    19.         GC.Collect();
    20.         GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
    21.         #endif
    22.     }
    23. }
    24.  
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,583
    Where is this GarbageCollectionManager from?

    Generally, disabling GC is only meaningful for a short period of time, ie within a frame. Or as this class implies, to manually run the garbage collector at specific points in time.

    I'd say you don't need that and shouldn't use it, since it seems to be incompatible with WebGL.

    Some background on garbage collection: https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)
    Essentially it's your mom cleaning up your room (ie memory) after you've been playing (ie allocating memory by instantiating objects). It's an automated process but will not necessarily happen at the most convenient time or frequency.
     
    MiguelAppStudio likes this.
  5. MiguelAppStudio

    MiguelAppStudio

    Joined:
    Nov 17, 2020
    Posts:
    19
    Gracias, lo tendré en cuenta.
    También encontré el foro del proyecto Garbage y estoy leyendo y estudiando un poco todos los comentarios.
    https://blog.unity.com/technology/feature-preview-incremental-garbage-collection

    Muchas gracias por ayudarme y guiarme en esto.
    Gracias