• 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 KRIEGSFRATZE · May 22, 2020 at 11:44 PM · roll a ball

Roll a ball simply will not move

I'm trying to move the ball with the player controller script, at the Environment and Player tutorial with the code exactly as written, but with an extra snippet to see if the script is getting the rigidbody component, and VS shows no errors. When I start the game, the ball will not move with either the arrow keys or wasd, and at the line that adds force to the rigidbody, rb.AddForce (movement * speed); , I keep getting an error that says NullReferenceException: Object reference not set to an instance of an object. I've seen t$$anonymous$$s question asked many times, but never answered. Here is the code I'm using, and there's no typos.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour { private Rigidbody rb;

 public float speed = 10.0f;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }
 void FixedUpdate()
 {
     float moveHorizontal = Input.GetAxis("Horizontal");

     float moveVertical = Input.GetAxis("Vertical");

     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

     rb.AddForce (movement * speed);

     if (rb = null)
     {
         Debug.Log("There is no rigidbody in " + transform.name);
     }
     else
         Debug.Log("Rigidbody component from gameobject is aquired.");
 }

}

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 blinkafrootable · May 23, 2020 at 01:01 AM 0
Share

When you double click on the NullReference error message, what line of code does it point to?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jasr_88 · May 23, 2020 at 08:56 PM

well... on the line 16 you have :

  if (rb = null)

T$$anonymous$$s is seting your rigidbody to null instead of compare it's value, you need to use the "==" operator.

The line 15 it's just fine, but only runs the first time, the second time the rb property is null (that's why you are getting the NullReferenceException)

Comment
Add comment · 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 KRIEGSFRATZE · May 23, 2020 at 09:26 PM 0
Share

That line was only to see if the rigidbody component from the player object was acquired in the script and print to the console if it was or not, it was, then I got a string of the nullreference exceptions.

avatar image Jasr_88 KRIEGSFRATZE · May 23, 2020 at 09:31 PM 0
Share

yes i know what are you trying to do, but you use the assign operator "=" (single equal character) instead of comparator "==" (double equal character) inside the if sentence

avatar image
-1

Answer by KRIEGSFRATZE · May 23, 2020 at 09:34 PM

I actually tried commenting out that if/else statement and now it suddenly works. Thank you all for helping me out. :D

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 Jasr_88 · May 23, 2020 at 09:36 PM 0
Share

you really need to diferentiate between "=" and "==" because that little "suddenly works" will get you in so many future troubles...

avatar image
0

Answer by Glacier1010 · May 23, 2020 at 01:23 AM

what line is it on? if u can say what line it's on I can help

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 Glacier1010 · May 23, 2020 at 01:24 AM 0
Share

did u add a rigid body to the object?

avatar image Glacier1010 · May 23, 2020 at 01:26 AM 0
Share

Everything looks fine

avatar image KRIEGSFRATZE · May 23, 2020 at 07:20 PM 0
Share

Yeah, I followed everything to the letter. The game will still run, but I keep getting that error and the ball still won't move. Pretty sure my input manager is ok too. Line 15 gives the error. Idk if it's a difference between unity versions or what because this particular issue has been asked about many times with no answer.

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

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

Related Questions

labyrinth like game 1 Answer

Error on Roll a Ball script Assets/scripts/playercontroller.cs(26,1): error CS8025: Parsing error 1 Answer

Finished Roll A Ball project, error? 1 Answer

Character triple+ Jumping: Please Help! 0 Answers

Virtual joystick for roll a ball game 0 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