• 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 aronthepalmer · Dec 20, 2019 at 02:42 PM · nullreferenceexceptionaddforce

NullReferenceException: Object reference not set to an instance of an object - I understand the issue but can't find the solution

Finally fixed a bunch of errors and pulled through only to find I got this basic ass problem that is stopping me from playtesting my scripts. I know this is a super common issue with new users, and that's exactly what I am, only my second game in the making so help is much appreciated.
The problem is in my Move method, it says I haven't set an instance of an object but I'm unsure on how to do so in this particular situation. All I'm trying to do is get my ball to finally get somewhere damn it! Thanks in advance!

Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public float moveSpeed;
     public float drag = 0.5f;
     public float terminalRotationSpeed = 25.0f;
     public Vector3 MoveVector { set; get; }
 
     private Rigidbody rb;
     private Transform cam_transform;
 
     void Start()
     {
         rb = gameObject.AddComponent<Rigidbody>();
         rb.maxAngularVelocity = terminalRotationSpeed;
         rb.drag = drag;
     }
 
     void Update()
     {
         MoveVector = PoolInput();
 
         MoveVector = RotateWithView();
 
         Move();
     }
 
     private void Move()
     {
         rb.AddForce(MoveVector * moveSpeed);
     }
 
     private Vector3 PoolInput()
     {
         Vector3 dir = Vector3.zero;
 
         dir.x = Input.GetAxis("Horizontal");
         dir.z = Input.GetAxis("Vertical");
 
         if (dir.magnitude > 1)
             dir.Normalize();
         return dir;
     }
 
     private Vector3 RotateWithView()
     {
         if(cam_transform != null)
         {
             Vector3 dir = cam_transform.TransformDirection(MoveVector);
             dir.Set(dir.x, 0, dir.z);
             return dir.normalized * MoveVector.magnitude;
         }
         else
         {
             cam_transform = GetComponent<ThirdPersonCamera>().camTransform;
         }
         return new Vector3();
     }
 }
 

alt text

egwdnsm-imgur.png (22.5 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 Neran28 · Dec 20, 2019 at 03:20 PM 0
Share

Already have a rb attached to the gameobject? Otherwise try assigning it using explicit cast. rb = gameObject.AddComponent() as RigidBody;

Show more comments
avatar image Priyanka-Rajwanshi · Dec 21, 2019 at 12:31 PM 0
Share

If you already have a rigidbody attached, use GetComponent ins$$anonymous$$d of AddComponent: rb = gameObject.GetComponent();

0 Replies

· Add your reply
  • Sort: 

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

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

How to add forward velocity to a Rigidbody2D? 2 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Does a new instance of a custom class return null? 2 Answers

How do i fix this : NullReferenceException: Object reference not set to an instance of an object Arrow.OnCollisionEnter 0 Answers

Button interactable not working via script 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