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

Creating Vectors on Update

Discussion in 'Scripting' started by Thom Sip, Dec 10, 2014.

  1. Thom Sip

    Thom Sip

    Joined:
    Apr 27, 2013
    Posts:
    26
    Hi guys,

    I recently came across a thread where someone said that creating Vectors on your Update function causes Unity's Garbage Collection to mess up ( can't find the thread anymore).

    Is this true? Is saying
    Code (CSharp):
    1. new Vector3(0,0,0)
    on Update bad practise?
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Not at all, but the general rule is - of course - to keep as much as possible out of the Update() method. :)

    Would you mind linking to the thread in question?
     
  3. Thom Sip

    Thom Sip

    Joined:
    Apr 27, 2013
    Posts:
    26
    I'm afraid I'm unable to find it. I remember stumbling across it when using google to search something. It was probably a very old topic.

    Thanks for the info.
     
  4. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
    Every time you use "new" for anything you allocate memory, even if it's temporary. Because of this, is a good idea to avoid "new" inside of the update function since it's called on every frame. Instead, if you need to use a vector inside of the update function it's more efficient to use a field variable that you can reuse on every frame.
     
  5. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    You might want to create the vector in your awake or start method and re use in your update, since you will allocate memory every time you use new, and discard the old vector object which will create a lot of garbage for the GC.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    All of you guys are forgetting that Vector3 is a struct and value types are only GC'd if they are fields of a reference type (or if you make a struct containing reference types - which you shouldn't do).

    Doing that every frame should just deallocate the memory on the stack when it goes out of scope and if you do what others are telling you by making it a variable in your class you're actually opening up the potential for additional GC if and when that class instance is destroyed.

    The long and short of it is - this is fine to do and you should be worrying about other things in your project :)
     
  7. Thom Sip

    Thom Sip

    Joined:
    Apr 27, 2013
    Posts:
    26
    Always Mixed messages on these kind of threads XD

    I'm gonna go with your answer Kelso, you seem to know what your talking about.

    Thanks!
     
  8. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Ya I can confirm kelso I wasn't aware that vector 3 was a struct not a class when I made my previous post.
     
  9. SUfIaNAHMAD_

    SUfIaNAHMAD_

    Joined:
    Jun 19, 2019
    Posts:
    18
    I think you are right because I came with some wonder as Unity was adding a new vector3 +transform.position in their Unity Official Project and if it is stack and it will auto adjust memory for unused data blocks so I believe it will not make a problem. Thank You
     
  10. SUfIaNAHMAD_

    SUfIaNAHMAD_

    Joined:
    Jun 19, 2019
    Posts:
    18
    if(needed){
    playerIndicator.transform.position = transform.position + new Vector3(0, 0.5f, 0);
    }