Objects sent to CoRoutines: are they pointers or references?

I’m passing in a GameOBject to a Coroutine:

StartCoroutine( gameMngr.SetLifeOverSoon(myObject) );

SetLifeOverSoon() destroys the object after 10 seconds depending on it’s color. However when testing it’s color, it always returns the color of the object at the time the Coroutine was called, even if the object has since had its color changed in the intervening 10 seconds.

Is a copy of the unchanged obeject stored?
In C++ etc. any changes made would be dynamically correct as the objects would be pointers.

Thanks for any clues. :slight_smile:

In C# there are only references and value-types, however a C# reference work similar to c++ references. Like a pointer you pass the address of the object, but you can’t access the address directly. The compiler just allows you to use the object behind the reference.

If you have a function parameter, it is always passed as the type it is, except you use ref, out keywords for valuetypes.

So if you pass a GameObject (which is a reference type) only the reference will be passed to the function and you can manipulate the GameObject directly.

If you pass a value-type (int, float, struct, Vector3) the value gets copied and the function just receives a copy of the original value. Changes to the parameter inside the function just affect this temp-copy.

Coroutine parameters work the same as regular function parameters or assignment statements.

“Simple” things, like ints, Colors, Vector3s, strings … are copied. Vector3 P3=P1; P3.y=0; turns P3 into a flat copy of P1. But in Transform T3=T1; T3.rotation=Quaternion.Euler(0,90,0);, T3 is a reference to T1, and no copies were made – T1’s rotatio was changed. If you need to copy a transform, you can create a new one and copy the position, rot and scale by hand.

You just have to know when you get a copy and when you get a reference (but anything “big,” which means a class, is a reference. A general trick is that if you need a reference you can usually “back up” one step. For example, transform.renderer.material is copied (I believe.) To pass a reference, pass just R=transform.renderer (renderers are passed by ref) and look at R.material.color.