Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

What an awesome programming language

Name: Anonymous 2012-10-09 14:34

Pretty much any language other than C#:


  gun.transform.localPosition.x = newPos;


C#:


  gun.transform.localPosition.x = newPos;

error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable


Suggested C# workaround:


  gun.transform.localPosition.x = new Vector3(newPos, gun.transform.localPosition.y, gun.transform.localPosition.z);


10/10 would bang

Name: Anonymous 2012-10-09 15:45

>>6

The only issue is that it makes perfect sense. Op is using a struct type in a getter/setter scenario. The following example should help it make some sense. The equivalent code in C++ would be:


int main(){
    ContainerClass c = new ContainerClass;
    c.SetVec( new Vector3(x,y,z));
    c.GetVec().X = 5;//since GetVec returns a valuetype, changing X to 5 has no affect on the Vector3 in ContainerClass
        //in order to prevent former java programmers from fucking themselves over because they don't know about structs, this would raise a compile error in C#
}

class ContainerClass{
private Vector3 _vec;//vector3 is a struct

public Vector3 GetVec(){return _vec;}
public void SetVec(Vector3 vec){_vec = vec}

}



The reason Java doesn't have this praoblem is that it treated everything as a reference type, the only value types are int, float, double, etc.

OP, the actual fix to your issue is to make gun.transform.localPosition a reference type, rather than a value type.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List