Search Unity

Saving time by reducing access?

Discussion in 'Scripting' started by EnsurdFrndship, May 19, 2017.

  1. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    Hello,
    I notice that AS I add more C sharp classes and scripts to my game, the longer it takes for Unity to compile all the scripts all together probably because each script can 'communicate' between all the other scripts (like how if I call a 'class.functionName()', and the function name is not found within the other script, the compiler generates errors). What if I want to 'limit' what gets 'accessed' between what scripts (to save compile time)? Other times, Unity freezes, and when I re-open Unity, minutes later, I see a dialog box that says, "Hold on compiling scripts." I believe it would save a lot of time and that from happening a lot, IF I could control what scripts can communicate. Is there a way to do this? Thanks.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I don't really understand the question, but if you're worried about compile time, you're worried about the wrong thing. It's hard to say if there would be a better structure for your code without seeing it, but if scripts have to talk to each other, you just have to accept that. As a project grows, it may take longer to compile or build.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can make more members/methods "private" which would limit their access, but I'm not sure whether that would actually affect compile time. Limiting member access is good coding practice, though, as it makes your classes perform more reliably (e.g. you won't accidentally change someNumber to a value that's inappropriate for that number to be, without going through the proper get/set functions). You can google 'encapsulation' for more info/discussion.
     
    KelsoMRK likes this.
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Demoting access won't have a huge impact on compile time and isn't a good reason for that decision anyway.

    If you can identify a chunk of your code that is slow moving (doesn't change often) then you can move it into a DLL and place that in your project.

    There have been a few threads about Unity improving their compilation by only doing deltas etc but to me the bigger win would be getting up to date with Mono/.NET and using the Roslyn compiler as a service
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Move classes that don't get changed much into something that compiles earlier.

    https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

    Plugins is typically used for this purpose. It will only recompile the folder if it changes.

    Controlling access between scripts is irrelevant to compile time and won't help out at all.