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 /
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 this 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)

This 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) ins$$anonymous$$d 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.

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

Animation clip through object 1 Answer

Help with UI text in the roll-a-ball tutorial? 1 Answer

What should I do if it shows this message ? Thank you 3 Answers

Problem with a ball to roll 0 Answers

CS0120 error 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