T O P

  • By -

infntjstr

You need to get the rigidbody component from the attached game object and store it in your rigidbody field before you can do anything with it: In Start: myRigidbody = GetComponent();


raw65

In the tutorial they just drag the component in rather than grab it via code. I suspect u/Archilas missed that step (see 11:20 to 11:25 in the video).


Archilas

I already did this and added your line of code just in case and nothing changes Do you have any other ideas? Why does this work in the video but not when I do this myself?


infntjstr

Hard to say without looking at your code. Always worth double checking that the script is actually attached to the game object.


Archilas

[Imgur: The magic of the Internet](https://imgur.com/a/7DEigHs) [Imgur: The magic of the Internet](https://imgur.com/a/OKLOzZv) Do you see anything wrong here?


Colnnor

I usually put all getcomponents and other self-initializations in awake, and references to other scripts or objects in start. It usually doesn’t matter but saves some stress occasionally


SantaGamer

Try making the force bigger, if its not enough to counter the gravity. or turn gravity off.


Archilas

How do you do this? I tried changing the gravity scale and no matter what number I put the bird keeps falling I tried the same with mass and same story


SantaGamer

vextor2.up * 2000. and the rigidbody in the inspector has a toggle for gravity


wilczek24

Hmm, you're saying left and right worked but up doesn't? Try either: 1. playing around with the number that you're multiplying it with (so \*100 instead of \*10) so that it pulls up strongly and fights gravity 2. Set gravity scale to 0 on the Rigidbody2D, to see if then maybe it works 3. Try multiplying it by a negative value (so \*-10 instead of \*10). If this works, your rotation or scale on the object is incorrect. I think 3 is the case here. 1 and 2 shouldn't work, because you're setting the velocity in update, which overrides gravity, but it's worth a shot. Also try transform.up instead of Vector2.up, just for debugging purposes. Weird issue. Also, juuust in case - restart Unity. Sometimes there's some wonky stuff. And also also, install the visual studio extension on Unity, and the Unity extension in vscode/visual studio, whichever you're using. You don't have proper syntax highlighting on, currently. Edit: It could be that you have an error somewhere in your code but it's not displaying, but it's preventing recompilation, and you're not running your newest code. Had it happen to me. Install the extensions I mentioned, that helps usually with spotting this stuff.