- Home /
 
converting javascript to c#
Hey, I ran into a problem and I don't know how I can fix this, this task is easy to do in javascript, but for me it's quite harder in c#
For an example of my problem I'll give you a part of the script in js:
     RightSki.rigidbody.AddRelativeTorque(0,- MovementSpeed, 0);
     LeftSki.rigidbody.AddRelativeTorque(0,- MovementSpeed, 0);
     LeftHand.hingeJoint.motor.force = MovementSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
     LeftLeg.hingeJoint.motor.force = - MovementSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
     LeftFoot.hingeJoint.motor.force = MovementSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
     LeftHand.hingeJoint.spring.spring = 1;
     LeftLeg.hingeJoint.spring.spring = 1;
     LeftFoot.hingeJoint.spring.spring = 1;
 
               So this works perfectly, but when I tried making it in c#:
         //Hand
         leftHandObject.hingeJoint.motor.force = MovementSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
         leftHandObject.hingeJoint.spring.spring = 1f;
         //Leg
         leftLegObject.hingeJoint.motor.force = -MovementSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
         leftLegObject.hingeJoint.spring.spring = 1f;
         //Feet
         leftFootObject.hingeJoint.motor.force = MovementSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
         leftFootObject.hingeJoint.spring.spring = 1f;
         //Skis
         leftSkiObject.rigidbody.AddRelativeTorque(0f,-MovementSpeed, 0f);
         rightSkiObject.rigidbody.AddRelativeTorque(0f,-MovementSpeed, 0f);
 
               when I did this in c# I get this error in most of the lines: error CS1612: Cannot modify a value type return value of UnityEngine.HingeJoint.motor'. Consider storing the value in a temporary variable So I tried making a temporary value that was like this: float motorForce1 = MovementSpeed  Input.GetAxis("Horizontal")  Time.deltaTime;` and replaced:
 leftHandObject.hingeJoint.motor.force = MovementSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
 
               with:
 leftHandObject.hingeJoint.motor.force = motorForce1;
 
               but I still get the same problem, so how do I get this to work?
thanks in advance
Answer by clunk47 · Sep 21, 2013 at 05:59 PM
You need to use a temp variable for the joint, as well as its motor property.
 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     HingeJoint joint;
     float motorforce1 = 5.0f;
     JointMotor motor;
     
     void Awake()
     {
         joint = GetComponent<HingeJoint>();    
         motor = joint.motor;
         motor.force = motorforce1;
     }
 }
 
 
              Thanks I'll try that tomorrow, can't do it right now, I'll give you a little update when I have time to do it
Your answer
 
             Follow this Question
Related Questions
What is this C# code in javascript 0 Answers
Help converting this to C# - a few issues 3 Answers
Distribute terrain in zones 3 Answers
GetComponent, What is it ? 1 Answer
Converting a javascript to C# 2 Answers