- Home /
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.");
}
}
When you double click on the NullReference error message, what line of code does it point to?
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)
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.
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
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
you really need to diferentiate between "=" and "==" because that little "suddenly works" will get you in so many future troubles...
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
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
![](https://koobas.hobune.stream/wayback/20220612235016im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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