Issues with Converting RopeScript to C#

So I’m converting the rope script (found here: http://www.unifycommunity.com/wiki/index.php?title=3D_Physics_Based_Rope) to C#, and I’ve run into an issue that doesn’t make sense to me. Most of it has converted fine but there are a few lines that are giving me trouble, namely lines 139 to 141 and 169 to 171, which both have the same problem.

Below are lines 139 to 141.

   end.lowTwistLimit.limit = lowTwistLimit; 
   end.highTwistLimit.limit = highTwistLimit; 
   end.swing1Limit.limit  = swing1Limit;

It is giving me the error on these three lines
“Assets/RopeScript.cs(171,15): error CS1612: Cannot modify the return value of `UnityEngine.CharacterJoint.swing1Limit’ because it is not a variable”

Which seems bizzare, since it handled the inherited variables from the few lines above it fine. Its like for some reason its not being treated as a CharacterJoint even though end has been set to that. What am I doing wrong?

A property is something that can be read and/or assigned like a variable, but it actually executes some code during the process. In C#, when a property is a structure or class, you can’t assign to its individual variables separately but rather you must assign the whole object at once. In your case, you need to get a copy of the existing property, modify the variable and then assign the copy back:-

SoftJointLimit temp = end.lowTwistLimit;
temp.limit = lowTwistLimit;
end.lowTwistLimit = temp;

In JS, when you assign a property’s variables, it automatically does all this for you.

Can you define getters/setters in UnityScript?

Regards to your comment above, you can do this fine if the data type is a class. If it’s a struct then it behaves as being described here where you have to reassign it. This is by design of how structs behave and work. Note though if you define it in C# as a field (rather than a property) it should work fine.

EDIT: Looking at the script I guess you can’t alter whether or not it’s using properties or fields. Just gotta assign over it with new instances I suppose. I’m guessing this doesn’t happen with UnityScript because it doesn’t natively support structs, and thus boxes everything?

Thanks andee! I totally forgot about properties. I’ve only seen them used once before.

The JS compiler can detect when you are accessing a field of a property and just makes the intermediate copy for you automatically.

Hi There !!!

I am trying to change the same rope files to C# and facing the same problem. If you guys have already worked out C# files, can u please share them or help me with this issue. Thanks in advance

Angela