Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MadJohny · Sep 21, 2013 at 05:52 PM · c#javascriptvariableconverttemporary

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · Sep 23, 2013 at 04:02 AM 0
Share

Were you able to try out a solution for this issue?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
     }
 }


 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadJohny · Sep 21, 2013 at 09:12 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges