Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

"Cost" of inactive scripts on GameObjects

Discussion in 'Scripting' started by UnbridledGames, Oct 16, 2020.

  1. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    I have menu bars, a prefab GameObject which used to contain a BarScript script on it that handled the display, reactions when it was clicked, etc.

    This object grew over time and is used for 5 different types of content, and the BarScript grew to become way too big.

    So I split the script up into BaseBarScript, and then each type got its own script that inherited from that.

    My problem:
    These bars are created, moved, and deleted a lot. So they are all a single prefab, and exist as a Pooled GameObject. Cool.

    Except now that the script is split into many different children scripts... that messes up the pooled item issue.

    I can either make 5 prefabs, with 5 different pools of bars (a bit more wasteful of memory), OR... I could put all 5 scripts on the bar prefab, leave them all inactive, then turn the correct one on when the bar is pulled from the pool (and turned back off when released).

    How much of an impact do inactive scripts have on memory? I'm trying to figure out whats worse: 5 different GameObject pools shuffling objects around, or keep the single pool and have 5 scripts, 4 of which are inactive, on each loaded and pooled bar.

    What impact does an inactive script on a gameobject have? It's still there completely, just not actively running Update and receiving messages?

    Alternately, what memory impact is there if a script is loaded/removed from the prefabs when they are loaded? For example, I need an Item Bar. The loader gets the refab, then attaches an ItemBarScript component to it and enables it.

    They all sound like a problem, I'm just not sure which is most impactful.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    100% of these questions can be answered by you attaching the profiler, available by Windows -> Profiler.

    Beware that the answer may vary greatly between Editor, PC, Mac, iOS, Android and WebGL. Performance is often very target sensitive.