why this code doesn't work ? c# ?

hi i want my cube when jump if i press (any key )
transform.position.y = number ;
that is my code

else    if ( Swipe_value < 0 )
                    {
                        float y = transform.position.y ;

                        y = -14.1365f ;
}

but this code doesnt work
any reason ?

Vector3 updatedPosition = transform.position;
updatedPosition.y = -14.1365f;
transform.position = updatedPosition;

So here you’re dealing with the difference between VALUE types and REFERENCE types. Pretty much, the Vector3 and the y float you get are copies so changing them will not affect the transform.

This is why we need to assign the entire Vector3 back to transform.position at once. Otherwise you are changing copies of values and not the actual values the transform is using.

http://www.albahari.com/valuevsreftypes.aspx

1 Like