Or does it not make much difference for optimization?
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.
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…
I should have been clear that it was a cached variable, not transform per say.
private void SendTransform()
{
ReceiveTransform(ref myTransform);
}
private void ReceiveTransform(ref Transform test)
{
}
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.
public class Foo1 : MonoBehaviour
{
public Foo2 Other;
void Start()
{
Transform tran = null;
Other.DoStuff(ref tran);
Debug.Log(tran.name, tran);
}
}
public class Foo2 : MonoBehaviour
{
public void DoStuff(ref Transform t)
{
t = this.transform;
}
}
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.
Thank you for the help.