Search Unity

Using a constant from another class in each frame?

Discussion in 'Scripting' started by unity_5fpsmegasupergiperprogramer, Sep 30, 2019.

  1. unity_5fpsmegasupergiperprogramer

    unity_5fpsmegasupergiperprogramer

    Joined:
    Dec 1, 2017
    Posts:
    101
    Hey, everybody. Do I understand correctly that constants are written directly into the code when creating Build? And they are optimized as much as possible? So, if I call a constant in each frame from another class:

    public static class Constants {

    public const ID = 6;

    }
    void Update() {

    int value = Constants.ID;

    }


    the load is minimal?
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Yes, you understand correctly. It would be the same as writing:
    Code (csharp):
    1.  
    2. int value = 6;
    3.  
     
    lordofduct and palex-nx like this.
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    In Unity, while in play mode, you may press Ctrl+7 (or Cmd+7 on Mac) and bring up the profiler window, it will show you slowest parts of your code to optimize. Also you may add Profiler.BeginSample() / EndSample calls to your code to add measurements to that window.