Rollerball Tutorial: rb.AddForce(movement * speed); not working?
Well I tried to research this issue to see how common it is and I've found a lot of errors mentioned that I don't seem to have. That being said, I can't find any errors elsewhere and I'm wondering if a second pair of eyes might help?
I am using C#, and the problem seems to come from line 24 based on the error I receive. I tried a couple different things as well, such as changing the command to rb.velocity, but it would not work because it can apparently not be used as a method.
Error:
NullReferenceException: Object reference not set to an instance of an object PlayerControls.FixedUpdate () (at Assets/Scripts/PlayerControls.cs:24)
Code: using UnityEngine; using System.Collections;
public class PlayerControls : MonoBehaviour
{
//Public or Private Declarations
public float speed = 100.0f;
private Rigidbody rb;
//initialization of movement
void start()
{
rb = GetComponent<Rigidbody>();
}
//application to each frame of movement
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal"); //x variable
float moveVertical = Input.GetAxis("Vertical"); //z variable
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}//end FixedUpdate
}//end Startusing UnityEngine;
With the first few lines being removed, it is line 21 in this post. $$anonymous$$y apologies.
Answer by Lrr2s · Sep 21, 2016 at 09:16 PM
This might seem patronizing, but did you change the move speed in the inspector? it starts at 0 by default and it took me forever to figure out what was going on when i did it.
Not patronizing at all. I did start using the speed control through unity. It worked initially. Then it stopped working once I started playing with camera as a child and such. After that I tried setting it in the code.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093200im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding proper air control to RigidbodyFirstPersonController 1 Answer
How do I get a character to walk on walls and ceilings? 1 Answer
Trouble carrying over scripted gravity to NPC. 0 Answers
Control Air movement with character controller 0 Answers
Why can my character controlled player can stand on the side of a block? 0 Answers