Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ahgr123 · Feb 15, 2016 at 05:50 PM · scripting problem

script for picking up objects error

Hi, i am beginner with scripting and i found this code on internet for picking up /throwing objects... I would like to make it work so i can use it as a example to learn javascript and see how things are done to make something similar.

But the code gives me following error. i tried searching but didnt found anything that fixed it.

alt text

And here is the code:

 var player : GameObject;
 var playcam : GameObject;
 var grabbed : Transform;
 var spring = 50.0;
 var damper = 5.0;
 var drag = 10.0;
 var angularDrag = 5.0;
 var distance = 0.2;
 var attachToCenterOfMass = false;
 var speed = 0.2;
 var throwForce : float;
 var throwRange : float;
 var oldSensY = 15;
 var oldSensX = 15;
 var rotate : float;
 
 private var vertRotAxis : Vector3;
  private var horizontalRot : float;
  private var verticalRot : float;
                  
 private var springJoint : SpringJoint;
 
 function Start ()
 {
 
 }
 
 function Update ()
 {
         rotate = Input.GetAxis("Rotate");
         
         // Make sure the user pressed the mouse down
         if (!Input.GetMouseButtonDown (0))
             return;
         if (!Input.GetMouseButtonDown (0))
             {
                 Camera.main.GetComponent("MouseLook").sensitivityX = oldSensX;
                 Camera.main.GetComponent("MouseLook").sensitivityY = oldSensY;
             }
         Screen.lockCursor = true;
         var mainCamera = FindCamera();
                 
         // We need to actually hit an object
         var hit : RaycastHit;
         if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 4))
                 return;
         // We need to hit a rigidbody that is not kinematic
         if (!hit.rigidbody || hit.rigidbody.isKinematic)
                 return;
         
         if (!springJoint)
         {
                 var go = new GameObject("Rigidbody dragger");
                 body = go.AddComponent.<Rigidbody>();
                 springJoint = go.AddComponent.<SpringJoint>();
                 body.isKinematic = true;
         }
         
         springJoint.transform.position = hit.point;
         if (attachToCenterOfMass)
         {
                 var 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);
 }
 
 function DragObject (distance : float)
 {
         var oldDrag = springJoint.connectedBody.drag;
         var oldAngularDrag = springJoint.connectedBody.angularDrag;
         springJoint.connectedBody.drag = drag;
         springJoint.connectedBody.angularDrag = angularDrag;
         var mainCamera = FindCamera();
         grabbed = springJoint.connectedBody.transform;
                                         
         oldYRotCam = mainCamera.transform.eulerAngles.y;
         oldYRotObj = springJoint.connectedBody.transform.eulerAngles.y;
         oldYRotComp = oldYRotCam - oldYRotObj;
 
         while (Input.GetMouseButton (0))
         {
                 newYRotCam = mainCamera.transform.eulerAngles.y;
                 newYRot = newYRotCam - oldYRotComp;
                 var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
                 springJoint.transform.position = ray.GetPoint(distance);
                 Physics.IgnoreCollision(player.GetComponent.<Collider>(), grabbed.GetComponent.<Collider>());
                 yield;
         if (Input.GetAxis("Rotate") == 0)
             {
                 horzRot = springJoint.connectedBody.transform.InverseTransformDirection(Vector3.up).normalized;
                 springJoint.connectedBody.transform.Rotate(horzRot, Input.GetAxis("Mouse X") * 15);
             }
             
                         
         if (distance > 5)
             {
                 distance = 5;
             }
         
         if (distance < 2)
             {
                 distance = 2;
             }
         
         if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance <= 5)
             {
                 distance += speed;
             }
         
         if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance >= 2)
             {
                 distance -= speed;
             }
         
         if (Input.GetAxis("Rotate") == 0)
             {
                 mainCamera.GetComponent("MouseLook").sensitivityX = oldSensX;
                 mainCamera.GetComponent("MouseLook").sensitivityY = oldSensY;
                 player.GetComponent("MouseLook").sensitivityX = oldSensX;
             }
                 
         if (Input.GetAxis("Rotate") != 0)
                {
                  player.GetComponent("MouseLook").sensitivityX = 0F;
                 Camera.main.GetComponent("MouseLook").sensitivityX = 0F;
                 Camera.main.GetComponent("MouseLook").sensitivityY = 0F;
         
                vertAxis = springJoint.connectedBody.transform.InverseTransformDirection(mainCamera.transform.TransformDirection(Vector3.right)).normalized;
                springJoint.connectedBody.transform.Rotate(vertAxis, Input.GetAxis("Mouse Y") * 6);
                
                horzAxis = springJoint.connectedBody.transform.InverseTransformDirection(mainCamera.transform.TransformDirection(Vector3.up)).normalized;
                springJoint.connectedBody.transform.Rotate(horzAxis, -Input.GetAxis("Mouse X") * 6);
                
                oldYRotCam = mainCamera.transform.eulerAngles.y;
               oldYRotObj = springJoint.connectedBody.transform.eulerAngles.y;
               oldYRotComp = oldYRotCam - oldYRotObj;
              }
         
         
             
         if (Input.GetMouseButtonDown (1))
             {
                 held = 0;
                 Physics.IgnoreCollision(player.GetComponent.<Collider>(), grabbed.GetComponent.<Collider>(), false);
                 grabbed = null;
                 mainCamera.GetComponent("MouseLook").sensitivityX = oldSensX;
                 mainCamera.GetComponent("MouseLook").sensitivityY = oldSensY;
                 player.GetComponent("MouseLook").sensitivityX = oldSensX;
                 springJoint.connectedBody.AddExplosionForce(throwForce,mainCamera.transform.position,throwRange);
                 springJoint.connectedBody.drag = oldDrag;
                 springJoint.connectedBody.angularDrag = oldAngularDrag;
                 springJoint.connectedBody = null;
                 StopCoroutine ("DragObject");
                 yield;
             }
         
         }
         if (springJoint.connectedBody)
         {
                 Physics.IgnoreCollision(player.GetComponent.<Collider>(), grabbed.GetComponent.<Collider>(), false);
                 grabbed = null;
                 springJoint.connectedBody.drag = oldDrag;
                 springJoint.connectedBody.angularDrag = oldAngularDrag;
                 springJoint.connectedBody = null;
         }
 }
 
 function FindCamera ()
 {
         if (GetComponent.<Camera>())
                 return GetComponent.<Camera>();
         else
                 return Camera.main;
 }

error.png (6.4 kB)
Comment
Add comment · Show 3
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 meat5000 ♦ · Feb 15, 2016 at 06:15 PM 0
Share

I dont see anyone fixing this. TL:DR situation with not enough info.

To learn Unity and coding go here

http://unity3d.com/learn/tutorials

avatar image LazyElephant · Feb 15, 2016 at 06:37 PM 0
Share

A null reference exception happens when an object isn't instantiated. In this script there are two possibilities for this: the player and playercam. Neither of these appear to be instantiated in the script, so you're supposed to link an object to them in the inspector. Did you do that?

avatar image ahgr123 · Feb 15, 2016 at 06:52 PM 0
Share

Yes i did add objects inside the inspector..you can try it in your project and you will see..thats why i dont know what could be wrong

1 Reply

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

Answer by EmHuynh · Feb 15, 2016 at 07:21 PM

Hello, @Araw. The problem could be caused by your if statements in your Update function. The first and second if statements are the same if( !Input.GetMouseButtonDown( 0 ) ) the if the condition of the first if statement is true (which it will be), it will make a return, causing the rest of your codes in the Update function to not execute. To resolve this issue, just remove the first if statement.

 function Update ()
  {
         ...
         // If condition is true, it will return
          if (!Input.GetMouseButtonDown (0))
              return;
         // If returned, the codes below this comment will not execute.
          if (!Input.GetMouseButtonDown (0))
              {
                  Camera.main.GetComponent("MouseLook").sensitivityX = oldSensX;
                  Camera.main.GetComponent("MouseLook").sensitivityY = oldSensY;
              }
          Screen.lockCursor = true;
          var mainCamera = FindCamera();
         ...
 }

Here's a mock-up of this problem:

 function Test()
 {
     // If condition is true, it will call return (think exit).
     if( !Input.GetMouseButtonDown( 0 ) ) {
         Debug.Log( "Returning!" );
         return; // Remove this statement and something different will happen.
     }

     // Won't get this far if returned.
     if( !Input.GetMouseButtonDown( 0 ) ) {
         Debug.Log( "Hello" );
     }
 
     Debug.Log( "World!" );
 }
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

39 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 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

First person shooter : Leaning! 2 Answers

NavMesh of an automatically generated maze 0 Answers

example of new way to load levels 1 Answer

Menu object not responding 0 Answers

What is the C# " public void (name) " equivalent in JS? 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