- Home /
Roll a Ball tutorial error
Here goes a rally stupid question, but I've tried everything I could thing of and everything I've read on here, and nothing works. I'm currently doing the 'Roll a Ball' tutorial and I'm onto the 2nd video, around 9:30 in. Despite typing word for word what the tutorial says, and after fixing it with the help from other questions, I still get this error:
"Assets/Scripts/playerController.cs(13,35): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)' "
It's driving me crazy... I've corrected all the spelling mistakes I could see and read about on here. Typing rigidbody in lowercase made the error stop. But I can't move the ball.
Heres my code: using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour
{
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Rigidbody.AddForce(movement);
}
}
Thanks.
Are you using Unity 4 or Unity 5? If using Unity 5 the tutorial code won't work.
Answer by Graham-Dunnett · Apr 08, 2014 at 11:54 AM
rigidbody.AddForce(movement);
(Note the lowercase r. RigidBody
is a class, rigidbody
is a member of type RigidBody
that belongs to the game object.)
Answer by setho246 · Mar 16, 2015 at 08:27 AM
Hey Crashmast,
If your using Unity 5, then here is your problem:
The tutorial is outdated, so you need to save the Unity Mono-develop, then close it.
Return to unity, in the Assets options drop-down box (up the top, near File, Edit etc.), click Run API Updater.... It will recommend that you make an backup, up to you whether or not you do.
This should fix your problem.
If your using unity 4 or later, then I can't help you sorry.
Setho246
Unfortunately no @ElieSaad, but I haven't been spending much time at all looking, as I lead quite a busy life, and Unity was just something to keep myself occupied whilst on the bus, on a plane, etc. If you happen to find any, though, I would be grateful if you could please tell me
Thanks,
Setho246
Hello @Setho246,
Im not sure why but it wont let me use the Run API Updater for some reason. Any fixes?
Thanks
Answer by Kiwasi · Mar 19, 2015 at 09:54 AM
The correct syntax in Unity 5 is
GetComponent<Rigidbody>().AddForce(movement);
You'll also have some trouble with GUIText later in the series. See this video for a fix.
Thanks a lot for this. I had the same problem and I was beginning to get really frustrated with it not working.
It's annoying that the tutorials haven't been updated but I suppose that forcing you to adapt is a good learning process anyway, considering I'm a total beginner when it comes to such things.
Answer by nidhi_30 · Jan 31, 2016 at 02:35 PM
using UnityEngine; using System.Collections;
public class playercontroller : MonoBehaviour {
//public float speed;
public 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 );
GetComponent<Rigidbody>().AddForce(movement);
}
} my code is this. and problem is :Cleaning up leaked objects in scene since no game object, component or manager is referencing them MonoScript has been leaked 1 times.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Error when I try to run the build in tutorial (roll a ball, tanks, and etc.) 0 Answers
roll-a-boll tutorial-rotator error 1 Answer
Error CS1502 Help! 1 Answer