Null Reference Exception..Don't know the answer...
Hi, I just installed unity today on my laptop and started with the roll-a-ball tutorial. Everything seemed to work fine until I reached "Moving the player" section on the list. Here is a link to it : Roll-a-Ball tutorial by unity
The problem is even though I have been following this tutorial very very carefully and closely, I get a Null Reference Exception error whenever I try to add a Force to the rigid body(jump to 12:50 in the video for this). When I play the scene, The console immediately gives me the Null Exception error at line 15( Which in my case is the rb.AddForce(movement); ). However, when I comment that line out, I get no errors but, obviously, that is useless as I am not able to move the player on screen. I have matched the code with the one in the tutorial. Here is the code from my unity environment for your reference :
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
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);
}
}
Any help is appreciated!!
Answer by Statement · Oct 22, 2015 at 03:19 PM
void start()
should be void Start()
with a capital S.
Yup. @kartikeyashukla , $$anonymous$$ethods/Classes/Structs are usually always capitalized, variables are not! :)
This is it. Since your Start()
won't happen it won't initialize the Rigidbody so it makes it that you can't add any force because it doesnt "exist".
Answer by exltus · Oct 22, 2015 at 02:08 PM
If "rb = GetComponent();" returns null it means, that you forgot to attach Rigidbody component to your player. Please double-check if gameobject, that have attached this script have attached Rigidbody component too.
Your answer
![](https://koobas.hobune.stream/wayback/20220612042255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I don't want the arms to turn with the ball at my game 0 Answers
calcule the distance in addforce. 0 Answers
How to control the speed of AddForce (ForceMode.Impulse)????? 1 Answer
My GameObject doesn't respond to anything 1 Answer
How can I find a point on a collider that is closest to my player. 0 Answers