Code problem in Block Breaker Tutorial [Solved]

Hi,

I’m at episode 75 of BlockBreaker, I think I entered the same code as instructed, at least I double-checked to make sure I didn’t forget a comma or something. But it seems to be the same. It’s the code that is supposed to make the ball bounce on the paddle (with a mouse click).

I get an error on line 26 (screenshot attached):
‘this.rigidbody2D.velocity = new Vector2 (2f, 10f);’

I tried to enter ‘GetComponent’ instead, like instructed in the MonoDevelop error message, but I can’t manage to fix this error. Maybe I’m writing it wrong. Does anybody know why it doesn’t work ?

Thank you

Hello Alex,

I have a sneaky suspicion you are using a more recent version of Unity than the course recommends :slight_smile:

Try changing line 26 to;

gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);

Hope this helps.


Updated Thu Jan 05 2017 14:52

Just realised that was a bit of a useless response as although it answered the question, it didn’t really explain anything - sorry!

With the newer version of Unity you can’t access components like you used to be able to from this, other than the transform (it is apparently special!).

So…

gameObject = the GameObject that the script is attached to
GetComponent<>() = gets a component of the specified Type within the < and > (case is important here)
Rigidbody2D = the Type of the component we are interested in
velocity = a public variable belonging to RigidBody2D representing the linear velocity of the Rigidbody

Hope that’s a bit more useful!

1 Like

Yep, that fixed it. Perfect.

Do you know which Unity version was used for the tutorials ? If I keep running into issues it will probably be easier to just download the correct version
thanks again :slight_smile:

1 Like

The first part of the course is taught using Unity 4.6, which you can download from the download archive on the Unity website. As the course progresses you will upgrade to Unity 5. I believe the GameDev.tv team have added some additional lectures / resources which indicate what needs to be changed in the code / games to make them work with Unity 5 if you have that installed (e.g. changes to the lectures etc).

To be honest, rather than installing an older version, if you already have Unity 5 installed you would be better off progressing with that in my opinion, you will invariably run into a few quirks like this, but either someone else will have posted about it here and had it answered, it’ll be covered in the additional info in the course, or, just pop another question up here and someone will help you out - that way you don’t need to move backwards before forwards again installing different versions. :slight_smile:

1 Like

Okay, sounds good. Thanks for the tip :slight_smile:

1 Like

You’re welcome. :slight_smile:

Privacy & Terms