Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Do you often send Transform with the ref keyword to methods?

Discussion in 'Scripting' started by Deleted User, Jun 26, 2017.

  1. Deleted User

    Deleted User

    Guest

    Or does it not make much difference for optimization?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It makes no difference because Transform is a reference type to begin with. And passing arguments by ref when they're values types won't net you anything in terms of performance anyway.
     
    Kiwasi and Deleted User like this.
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    I'm curious about under what conditions you were able to pass a transform as a ref? Since it's a property in both GameObject and Component, you wouldn't normally be able to pass it with a ref keyword.

    To be fair, if you're passing a massive struct to a function, a ref can make sense. Of course, if the struct was massive enough to actually create a performance hit, I'd question why that struct needed to exist at all...
     
    Deleted User likes this.
  4. Deleted User

    Deleted User

    Guest

    I should have been clear that it was a cached variable, not transform per say.

    Code (CSharp):
    1.  
    2.  
    3.  
    4.     private void SendTransform()
    5.     {
    6.         ReceiveTransform(ref myTransform);
    7.     }
    8.  
    9.  
    10.     private void ReceiveTransform(ref Transform test)
    11.     {
    12.  
    13.     }
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    When using the 'ref' keyword for a method parameter of a reference type. Basically you're you're doing is allowing the called method access to the field/var from the calling scope.

    Code (csharp):
    1.  
    2. public class Foo1 : MonoBehaviour
    3. {
    4.    
    5.     public Foo2 Other;
    6.    
    7.     void Start()
    8.     {
    9.         Transform tran = null;
    10.         Other.DoStuff(ref tran);
    11.         Debug.Log(tran.name, tran);
    12.     }
    13.    
    14. }
    15.  
    16. public class Foo2 : MonoBehaviour
    17. {
    18.    
    19.     public void DoStuff(ref Transform t)
    20.     {
    21.         t = this.transform;
    22.     }
    23.    
    24. }
    25.  
    For example, here what will happen is that the log in Foo1 will display the name of the Transform found on Foo2.

    Basically ref will work like 'out', but will not require the setting of the parameter.

    It gives no real optimization difference (it may shorten the stack? I'll have to dig into the IL to see, but it's little to no gain if it did).

    Use ref for what it's intended for, not for some naive attempt at optimizing code.
     
    Kiwasi and Deleted User like this.
  6. Deleted User

    Deleted User

    Guest

    Thank you for the help.