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 NinkaDuck24 · Jan 29, 2016 at 05:34 PM · nullreferenceexception

Help needed finding NullReferenceException

Hi, I'm very new to C# and Unity, I've been slowly learning it as a new years resolution. I recently set out to make a game which had a bunch of things I wanted to learn. How to make a main menu, crosshair, ect...

I figured this would be the easiest way to learn all the different areas which create a game. My game uses the FPSController, you can pick up objects, throw them with left click, rotate with right click, flick your mouse and let go to also throw. It works perfectly apart from a NullReferenceException. I followed 2 different YouTube tutorials and took bits of code from each tutorial and mashed them together to get what I wanted. Anyway, here's my Frankenstein code, this is also my first post on here so apologies if I format anything wrong.

This is the line of code which Visual Studio flags up as the issue previousGrabPosition = carriedObject.transform.position;

And here's the entire thing.

 public class PickupObject : MonoBehaviour {

 GameObject mainCamera;
 GameObject carriedObject;

 Vector3 previousGrabPosition;
 Vector3 previousRotatePosition;

 public float distance;
 public float smooth;

 bool carrying;
 float chargeTime = 0;

 void Start () {
     mainCamera = GameObject.FindWithTag("MainCamera");
 }

 void Update () {
     if(carrying) {
         carry(carriedObject);
         checkDrop();
         if (Input.GetMouseButton(1))
         {
             rotateObject();
         }
     } else {
         pickup();
     }
     previousGrabPosition = carriedObject.transform.position;

     if (Input.GetMouseButton(0))
         chargeTime += Time.deltaTime;

     if (Input.GetMouseButtonUp(0))
     {
         float pushForce = chargeTime * 20;
         pushForce = Mathf.Clamp(pushForce, 1, 15);
         DropObject(pushForce);
         chargeTime = 0;
     }
 }

 void rotateObject() {
     carriedObject.transform.Rotate(0,4,0);
 }

 void carry(GameObject o) {
     o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
 }

 void pickup() {
     if (Input.GetKeyDown(KeyCode.E)){
         int x = Screen.width / 2;
         int y = Screen.height / 2;

         Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit)) {
             Pickupable p = hit.collider.GetComponent<Pickupable>();
             if(p != null) {
                 carrying = true;
                 carriedObject = p.gameObject;
                 //p.gameObject.rigidbody.isKinematic = true;
                 p.gameObject.GetComponent<Rigidbody>().useGravity = false;
             }
         }
     }
 }

 void checkDrop() {
     if(Input.GetKeyDown (KeyCode.E)) {
         DropObject(0);
     }
 }

 void DropObject(float pushForce)
 {
     if (carriedObject == null)
         return;

     Rigidbody rb = carriedObject.gameObject.GetComponent<Rigidbody>();
     carrying = false;
     rb.useGravity = true;
     Vector3 throwVector = carriedObject.transform.position - previousGrabPosition;
     float speed = throwVector.magnitude / Time.deltaTime;
     Vector3 throwVelocity = speed * throwVector.normalized;

     throwVelocity += Camera.main.transform.forward * pushForce;

     rb.velocity = throwVelocity;
     carriedObject = null;
 }

}

Comment
Add comment
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

1 Reply

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

Answer by flashframe · Jan 29, 2016 at 09:00 PM

Try moving that line to your if(carrying) block

 if(carrying) {
          carry(carriedObject);
          checkDrop();
          if (Input.GetMouseButton(1))
          {
              rotateObject();
          }
          previousGrabPosition = carriedObject.transform.position;
      } else {
          pickup();
      }
      

EDIT:

You also need to swap two lines around so that the game object is stored before the update loop runs.

   carriedObject = p.gameObject;
   carrying = true;
Comment
Add comment · Show 3 · 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 NinkaDuck24 · Jan 29, 2016 at 10:51 PM 0
Share

That worked, it's now not constantly updating 60 times per second! Thank you! The NullReferenceException error now appears when I drop an object that I was carrying on the carrying = ture; line, line 62.

Any ideas why that's getting flagged up?

avatar image flashframe NinkaDuck24 · Jan 30, 2016 at 12:25 AM 0
Share

You need to swap those two lines around so that the game object is stored before the update loop runs.

  carriedObject = p.gameObject;
  carrying = true;
avatar image NinkaDuck24 flashframe · Jan 30, 2016 at 01:39 AM 0
Share

Thank you, that has fixed everything :) Awesome for helping so fast too!

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

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

Related Questions

NullReferenceException when using gameObject.SetActive(false); 0 Answers

This script was functioning perfectly then i turned it back on and it is giving me a null reference error 1 Answer

null reference exception 0 Answers

Random Chance NullReferenceException: Object reference not set to an instance of an object 2 Answers

NullReference.Exveption: Object reference not set to an instance of an object 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