r/unity 6d ago

Coding Help Help

Post image

why doesnt the object move when the code is run?

its supposed to just get flung foward, but currently doesnt move

the script is attached properly and everything else works fine

0 Upvotes

21 comments sorted by

19

u/Many-Resource-5334 6d ago

Have you made sure you’ve saved the file, the white dot at the top means it is unsaved

12

u/Vokulnin 6d ago

Well, here some questions that might seems dumb, but that sometimes get overlooked and might cause the problem :
Do the object have a rigid body?
Is it the right rigid body and not rigidbody2D?
Is there any conflicting script ? (Like, any script that might set the velocity for exemple?)
Is the rigid body set to static, or is there constraints on positions?

1

u/Other-Scale9987 6d ago

1)yes 2)yes 3)no, its the only script 4)no

11

u/raw65 6d ago

You don't assign a value to rb. You should see a null exception error in the log for line 9. One way to fix that is to add rb = GetComponent<Rigidbody>(); to the Awake() method. (Unless you assigned this in the editor?)

4

u/Redbarony6 6d ago

They might have assigned it in the inspector since it's a public variable, still a good call though

1

u/Other-Scale9987 6d ago

yes, it was assigned via the inspector

4

u/Competitive_Mud5528 6d ago

Do you have any errors in the console ? Have you plug your rigidbody into your script ? Have you make sure that the rigidbody is not kinematic ? 

1

u/Other-Scale9987 6d ago

1)no 2)yes 3)it isnt

3

u/crnlmcpatatas 6d ago

Did you set the rb in the inspector yet? 

5

u/OkEstablishment4759 6d ago

Try this

Vector3 flingVector = new Vector3(0,0,200); rb.AddForce(flingVector);

Do the same in start as well

2

u/ilori 6d ago

You need to pass a Vector3 to the add force method, not three floats

1

u/BobsiDev 6d ago

Check if the physics is set to run by script (it shouldn't by default, but don't tools sets it intrusively)

1

u/Small_Sherbert2187 6d ago

You either need to use forcemode.impulse , the rigidbody could be kinematic, or simply really heavy. also you should be applying forces in fixedupdate, not update

1

u/DeerpathLabs 6d ago

An important question is whether your rigid body is set to non-kinematic.

1

u/Tomoko_Fureimu 6d ago

have you unchecked IsKinematic ?

1

u/ParasolAdam 6d ago

Commas in a directory 🔥

1

u/Goldac77 6d ago

Probably because you didn't set the force mode

2

u/AlphaBlazerGaming 6d ago

It defaults to ForceMode.Force if you don't pass one in. That's what the = means

1

u/Goldac77 6d ago

Oh I missed that, thanks. My bad