- Home /
The question is answered, right answer was accepted
Errors when following the Rolling Ball script tutorial.
Very new to Unity, but not to making games. I am a Designer/Producer/Artist, not a programmer, though. Sorry if this is a dumb question, but I cannot find the answer when I search, so I am posting.
I am trying to do the Roll-A-Ball tutorial here: http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player. I have followed every step through creation of the script (below) and when he saves his script (about 9:50) he then proudly says that there are no errors in the console. But, I have errors.
I am sure I did something wrong, but cannot for the life of me figure out what. Here's the script:
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);
}
}
But I get this error at the end: Assets/Scripts/PlayerController.cs(13,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)'
Answer by meat5000 · Sep 21, 2013 at 10:34 PM
Try a small r on rigidbody.
Wow... I saw the IDE offer R as a preset and took it. Thank you very, very much!
You are welcome. Accept the answer if it solved your problem :)
what is r
i am similar this problem but i can not understand what u talk
plz help me
rigidbody, not Rigidbody.
Use capital R when casting the type, lowercase r when referring to a rigidbody instance.
Answer by agustina · Feb 27, 2014 at 06:17 AM
I have the same problem as you, then i change Rigidbody into rigidbody, the eror is done, but when i play the scene , the object cant move, how can i fix that?
Here is a copy of a working PlayerController script for that project:
using UnityEngine; using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour { public float speed; public GUIText countText; public GUIText winText; private int count;
void Start()
{
count = 0;
SetCountText();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "PickUp")
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 12) {
winText.text = "YOU WON!";
}
}
}
Hope it helps
This was my solution.
GetComponent().AddForce (movement);
Yeah they updated the API without updating the tutorial. If you put the rigidbody.AddForce(movement); AddForce should show up in red. Save your script then switch back to unity and it will tell you that you're using old references and ask you to update. If you do it will switch your code to
`GetComponent<Rigidbody>().AddForce(movement);`
Hope this helps
[UNITY 5] Do not be miscontrued :
The following cache still works as it has always done
Rigidbody rbody;
// Use this for initialization
void Start ()
{
rbody = GetComponent<Rigidbody>();
}
Just call your rigidbody something other than 'rigidbody' or you will get an error.
Hey, Rodney. I'm also new to Unity and figured this out using the documentation.
Before any of the fixedUpdate stuff, you need to initialize the ball -- or Rigidbody -- and give it a name. Then you reiterate that name ins$$anonymous$$d of "rigidbody" before AddForce. Here's my code:
public class PlayerController : $$anonymous$$onoBehaviour
{
public Rigidbody sam;
void Start (){
sam = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
sam.AddForce(movement);
}
}
As you can see, I named my ball Sam. Hope this helps!
Aleshia,
I am very new to Unity. After trying about a dozen failed attempts YOURS is the only one that worked on Unity 5 for me. Don't really understand code, but your answer makes sense and worked perfect. Now I have an object named sam.
Thanks so much.
Follow this Question
Related Questions
Error in script 1 Answer
Error CS1502 Help! 1 Answer
Why is rigidbody.AddForce not working? 3 Answers
Error CS1502 help! 1 Answer