Search Unity

Component overhead in gameObjects.

Discussion in 'Editor & General Support' started by screenname_taken, Feb 17, 2021.

  1. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Hi guys, i have a really noob sounding question.
    I'm porting a very old JS project to C# and i'm noticing of course lots of unnecessary stuff in my making of that project.
    Like control cubes with scripts, and instead of just removing the renderer component and the mesh and just leaving the script and transform, or any triggers needed, i placed a script that would disable the renderer of said object.
    If i have for example 10 of those objects in a scene, that's 30 extra components. Does it make much of a difference in performance or load times? Or in RAM usage?
    I would consider it good practice to just remove those components, but does anyone know if there is an actual difference?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    No, but you can know by attaching the profiler. Window -> Analysis -> Profiler.

    And it will only be useful on the actual built target, NOT by running it in the editor.

    Remember every action you take (such as to remove a thing) may have consequences and will have costs.

    If the thing isn't causing problems, leave it in place until everything else is sorted out and you're done with the port.
     
    Joe-Censored likes this.
  3. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    i'm only removing that component that i see i have a script that removes it on scene load, i was thinking that the script removing it might cause garbage and stuff.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    10 GameObjects / 30 components is pretty trivial. Might be hard to see that showing up in the profiler in practice, but yes technically they are using some additional resources. Try creating a few thousand of them, profile that in a build, and you'll get a better idea what you're even looking for.

    It can cause GC. You could consider manually calling GC collect after doing so, before your game gets moving. A little more info on that:
    https://forum.unity.com/threads/manual-garbage-collection-best-practices.534275/
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My opinion though is you're over optimizing. It is better to spend time working on real problems you're encountering than working towards maximum theoretical optimization to prevent potential problems you haven't seen yet. YMMV
     
    Kurt-Dekker likes this.
  6. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Probably yeah. From what you are both telling me i'm thinking it too much.
    I mean even back then i was calling GC manually right after level load and before fading the camera in.
    It was an android game and i was worrying of not taxing stuff too much an draining batteries.
    Did everything i could figure out with the fps and the accelerometer reading rate. tried to disable anything not needed.
     
    Joe-Censored likes this.