- Home /
BlockBreaker Tutorial: CS0120 Error.
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public Paddle paddle;
private Vector3 paddleToBallVector;
private bool hasStarted = false;
// Use this for initialization
void Start () {
paddleToBallVector = this.transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update () {
if (!hasStarted) {
this.transform.position = paddle.transform.position + paddleToBallVector;
if (Input.GetMouseButtonDown (0)) {
print ("Mouse Clicked, launch ball.");
hasStarted = true;
Ball.rigidbody2D.velocity = new Vector2(2f, 10f);
}
}
}
}
It seems that there is something wrong in my Ball.rigidbody2D.velocity = new Vector2(2f,10f); line (CS0120) and I have no idea. The guys who made that tutorial has no problem there, but I've got "velocity" with red, telling me it has no definition for "velocity" (error CS0117) ... Any ideas?
The line is 26 (26, 30).
it's quite likely that the tutorial was made using an earlier version of unity which contained a shortcut for rigidbody2D.... those have been removed for the more recent versions so you'll need to get the component yourself before accessing. the unity docs/tutorials should show you how.
in future, to help people helping you, please post the complete error message including line numbers.
the tutorial was made with unity 4.6, so... I guess it should work. Well... at first I had one more error because ins$$anonymous$$d of Ball.rigidbody2D... was this.rigidbody2D... so I changed that and that error was solved, but this one...
The line is 26 (26, 30).
" I guess it should work"... no! unless you're running the same version, assume nothing ;) and if you're running 5.x - then it will NOT work.
there's no rigidbody2D variable defined within the Ball class - which is what line 26 specifies.
if this script is attached to the ball then you don't need to do anything but get the Rigidbody2D component and refer to it... so something like:
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
although you should cache the component reference in Awake()/Start() to avoid doing it every time.
Your answer
Follow this Question
Related Questions
Error CS0117 1 Answer
Unity iPhone - StreamReader.EndOfStream Not Working? 1 Answer
error CS0117: `Resources' does not contain a definition for `Load' 1 Answer
Unity doesn't recognize any definition for Network 1 Answer
Help with more code 2 Answers