• 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
Question by Ochreous · May 14, 2014 at 05:44 AM · c#rigidbodydrag

C# DragRigidbody ArgumentException: get_main can only be called from the main thread

I'm trying to convert DragRigidbody into C# and I've encountered a problem. when I clicked my left mouse button I get the following error ArgumentException: get_main can only be called from the main thread. Following that Unity freezes and crashes. So I couldn't read what was causing the error from the console. Any idea what is wrong with the script?

 using UnityEngine;
 using System.Collections;
 
 public class DragRigidbody : MonoBehaviour {
 
     public float spring = 50.0f;
     public float damper = 5.0f;
     public float drag = 10.0f;
     public float angularDrag = 5.0f;
     public float distance = 0.2f;
     public bool attachToCenterOfMass = false;
     public Camera mainCamera = Camera.main;
     public RaycastHit hit;
     private SpringJoint springJoint;
     
     void Update ()
     {
         // Make sure the user pressed the mouse down
         if (!Input.GetMouseButtonDown (0))
             return;
         
         // We need to actually hit an object
         if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 100))
             return;
         // We need to hit a rigidbody that is not kinematic
         if (!hit.rigidbody || hit.rigidbody.isKinematic)
             return;
         
         if (!springJoint)
         {
             GameObject go = new GameObject("Rigidbody dragger");
             Rigidbody body = go.AddComponent ("Rigidbody") as Rigidbody;
             springJoint = go.AddComponent("SpringJoint") as SpringJoint;
             body.isKinematic = true;
         }
         
         springJoint.transform.position = hit.point;
         if (attachToCenterOfMass)
         {
             Vector3 anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
             anchor = springJoint.transform.InverseTransformPoint(anchor);
             springJoint.anchor = anchor;
         }
         else
         {
             springJoint.anchor = Vector3.zero;
         }
         
         springJoint.spring = spring;
         springJoint.damper = damper;
         springJoint.maxDistance = distance;
         springJoint.connectedBody = hit.rigidbody;
         
         StartCoroutine ("DragObject", hit.distance);
     }
 
     void DragObject (float distance)
     {
         float oldDrag = springJoint.connectedBody.drag;
         float oldAngularDrag = springJoint.connectedBody.angularDrag;
         springJoint.connectedBody.drag = drag;
         springJoint.connectedBody.angularDrag = angularDrag;
         Camera mainCamera = Camera.main;
         while (Input.GetMouseButton (0))
         {
             Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
             springJoint.transform.position = ray.GetPoint(distance);
         }
         if (springJoint.connectedBody)
         {
             springJoint.connectedBody.drag = oldDrag;
             springJoint.connectedBody.angularDrag = oldAngularDrag;
             springJoint.connectedBody = null;
         }
     }
 }
 
Comment
gonzam88

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · May 14, 2014 at 06:34 AM

Problem #1, you need to move the initialization of this variable to Start(). So you will declare the variable:

  public Camera mainCamera;

Then you will add:

 void Start() 
 {
     mainCamera = Camera.main;
 }

Problem #2 is that you've removed the 'yield' from line 68. That's what is causing your script to hang Unity. So just below line 67, insert:

 yield return null;

Now that you are using 'yield', 'DragObject' needs to be and IEnumerator. So line 57 becomes:

 IEnumerator DragObject (float distance)

And I would change the StartCoroutine() call on line 54 to:

 StartCoroutine (DragObject(hit.distance));

Make these changes, and your code will work.

Comment
gonzam88

People who like this

1 Show 2 · 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 Ochreous · May 14, 2014 at 07:13 AM 0
Share

Unity's no longer crashing but I'm still getting the same argument exception from the console

 using UnityEngine;
 using System.Collections;
 
 public class DragRigidbody : MonoBehaviour {
 
     public float spring = 50.0f;
     public float damper = 5.0f;
     public float drag = 10.0f;
     public float angularDrag = 5.0f;
     public float distance = 0.2f;
     public bool attachToCenterOfMass = false;
     public Camera mainCamera = Camera.main;
     public RaycastHit hit;
 
     private SpringJoint springJoint;
 
     void Start()
     {
         mainCamera = Camera.main;
     }
 
     void Update ()
     {
         // Make sure the user pressed the mouse down
         if (!Input.GetMouseButtonDown (0))
             return;
         
         // We need to actually hit an object
         if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 100))
             return;
         // We need to hit a rigidbody that is not kinematic
         if (!hit.rigidbody || hit.rigidbody.isKinematic)
             return;
         
         if (!springJoint)
         {
             GameObject go = new GameObject("Rigidbody dragger");
             Rigidbody body = go.AddComponent ("Rigidbody") as Rigidbody;
             springJoint = go.AddComponent("SpringJoint") as SpringJoint;
             body.isKinematic = true;
         }
         
         springJoint.transform.position = hit.point;
         if (attachToCenterOfMass)
         {
             Vector3 anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
             anchor = springJoint.transform.InverseTransformPoint(anchor);
             springJoint.anchor = anchor;
         }
         else
         {
             springJoint.anchor = Vector3.zero;
         }
         
         springJoint.spring = spring;
         springJoint.damper = damper;
         springJoint.maxDistance = distance;
         springJoint.connectedBody = hit.rigidbody;
         
         StartCoroutine(DragObject(hit.distance));
     }
 
     IEnumerator DragObject (float distance)
     {
         float oldDrag = springJoint.connectedBody.drag;
         float oldAngularDrag = springJoint.connectedBody.angularDrag;
         springJoint.connectedBody.drag = drag;
         springJoint.connectedBody.angularDrag = angularDrag;
         Camera mainCamera = Camera.main;
         while (Input.GetMouseButton (0))
         {
             Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
             springJoint.transform.position = ray.GetPoint(distance);
             yield return null;
         }
         if (springJoint.connectedBody)
         {
             springJoint.connectedBody.drag = oldDrag;
             springJoint.connectedBody.angularDrag = oldAngularDrag;
             springJoint.connectedBody = null;
         }
     }
 }
 
avatar image robertbu · May 14, 2014 at 12:20 PM 1
Share

The exception is because you did not fully make the first change. Line 12 should be:

 public Camera mainCamera;

The whole issue was the assignment of Camera.main.

avatar image

Answer by gonzam88 · Apr 20, 2016 at 06:16 AM

excellent. been looking for this. the old js script sucks. Final working code below:

 using UnityEngine;
 using System.Collections;
 
 public class dragRigidbody : MonoBehaviour {
 
     public float spring = 50.0f;
     public float damper = 5.0f;
     public float drag = 10.0f;
     public float angularDrag = 5.0f;
     public float distance = 0.2f;
     public bool attachToCenterOfMass = false;
     public Camera mainCamera;
     public RaycastHit hit;
 
     private SpringJoint springJoint;
 
     void Start()
     {
         mainCamera = Camera.main;
     }
 
     void Update ()
     {
         // Make sure the user pressed the mouse down
         if (!Input.GetMouseButtonDown (0))
             return;
 
         // We need to actually hit an object
         if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 100))
             return;
         // We need to hit a rigidbody that is not kinematic
         if (!hit.rigidbody || hit.rigidbody.isKinematic)
             return;
 
         if (!springJoint)
         {
             GameObject go = new GameObject("Rigidbody dragger");
             Rigidbody body = go.AddComponent <Rigidbody>() as Rigidbody;
             springJoint = go.AddComponent<SpringJoint>() as SpringJoint;
             body.isKinematic = true;
         }
 
         springJoint.transform.position = hit.point;
         if (attachToCenterOfMass)
         {
             Vector3 anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
             anchor = springJoint.transform.InverseTransformPoint(anchor);
             springJoint.anchor = anchor;
         }
         else
         {
             springJoint.anchor = Vector3.zero;
         }
 
         springJoint.spring = spring;
         springJoint.damper = damper;
         springJoint.maxDistance = distance;
         springJoint.connectedBody = hit.rigidbody;
 
         StartCoroutine(DragObject(hit.distance));
     }
 
     IEnumerator DragObject (float distance)
     {
         float oldDrag = springJoint.connectedBody.drag;
         float oldAngularDrag = springJoint.connectedBody.angularDrag;
         springJoint.connectedBody.drag = drag;
         springJoint.connectedBody.angularDrag = angularDrag;
         Camera mainCamera = Camera.main;
         while (Input.GetMouseButton (0))
         {
             Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
             springJoint.transform.position = ray.GetPoint(distance);
             yield return null;
         }
         if (springJoint.connectedBody)
         {
             springJoint.connectedBody.drag = oldDrag;
             springJoint.connectedBody.angularDrag = oldAngularDrag;
             springJoint.connectedBody = null;
         }
     }
 }
Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image

Related Questions

RigidBody Script Conflict 1 Answer

Parenting Preventing Player Rotation 0 Answers

C# Need Help converting from Rigidbody to Character Controller 0 Answers

C# RotateAround for 2DRigidBodies 1 Answer

Variable in another is not getting called; stopping next action 1 Answer


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