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

Untiy 5.0 performance questions

Discussion in 'Editor & General Support' started by btowle, Mar 12, 2015.

  1. btowle

    btowle

    Joined:
    Dec 31, 2014
    Posts:
    11
    So I tried to upgrade my project from Unity 4.5 to Unity 5.0.
    After that happened, I noticed my character responce was very sluggish.

    I am creating an authoritarian server and having the players RPC their commands to their hero.
    Now I am not so vain to think my code is perfect but my question here is why did it work great in Unity 4.5 (in fact I had to skip 4/5 update cycles) and it perform so horribly with Unity 5.0?

    The only suspicion I had was regarding the module access. I could be wrong but this is what I thought was going on in C#:
    <Old method>
    this.rigidbody - a pointer to a module that Unity assumed would be there.
    <New Method>
    this.gameobject.getComponent<Rigidbody>() - a template function that would search for that type of variable inside the game object.

    Now I understand the dilemma for not allowing the old method, but won't it take far more CPU cycles to invoke the template function rather than look for the pointer. I could be wrong, but I can't think of any other reason I would get such horrible performance.

    I am currently working on a game that I want to release for an Indie startup. Before I make the choice to commit to 5.0 I need to understand whats going on here.

    Thank in advance,
    Btowle
     
  2. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    if you're using the component often, you should cache it and only call the .GetComponent<Rigidbody>() from Awake or Start,
     
  3. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    Code (CSharp):
    1. Rigidbody m_rigidbody;
    2. void Awake() {
    3. m_rigidbody = GetComponent<Rigidbody>();
    4. }
    5. void Update() {
    6.  // use m_rigidbody instead of this.rigidbody
    7. }
     
    btowle likes this.
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Considering Unity 4 looked the component up internally, I doubt this is the reason you're experiencing slowdown. Use the profiler freely available to you instead of guessing.
     
    btowle likes this.
  5. btowle

    btowle

    Joined:
    Dec 31, 2014
    Posts:
    11
    Thanks, Shkarface I will give that a try. I am still curious why it worked so well in 4.5 though.
    Mr. Hippocoder, noob question where would I find this profiler?

    Thanks,
    btowle
     
  6. btowle

    btowle

    Joined:
    Dec 31, 2014
    Posts:
    11
    Never mind found it. I will look at it.
     
  7. btowle

    btowle

    Joined:
    Dec 31, 2014
    Posts:
    11
    Thanks to both. Thins are running much smoother now. I had no idea about the profiler!
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Sort the profiler columns by Time MS - then when you play, you get to see which scripts take up the most time at the top. There's also deep profiling etc - just check the docs.
     
    shkar-noori likes this.
  9. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    I didn't remember that the Profiler is now free :confused: