Can't AddForce to Insantiated Object

Hi my prefab has RigidBody2d and Collider2D attached
I try to instantiate that prefab and initial force(ennemy’s bullet)
but i dont see any Force effect on the instantiated object.
Could sombody take a quick look?

function FixedUpdate(){shoot();}


function shoot(){
if(ready_to_shoot){ready_to_shoot=false;
                       
                         var arrr= Instantiate(trol_arrow, miejste_strzaly.position, miejste_strzaly.rotation );
                         arrr.GetComponent.<Rigidbody2D>().AddForce(Vector3(10,0,0));
yield WaitForSeconds(1);
ready_to_shoot=true;

}}

Hmm, I have the feeling there’s quite a lot wrong with the above code, even with my noob eyes. :wink:
First I’m 99.99% percent certain AddForce needs a new Vector3() and since it’s used with a Rigidbody2D I think a Vector2() would probably suffice.

Second, a yield construction won’t work in a normal function, you need an IEnumerator for that to work and the correct syntax would then be: yield return new WaitForSeconds(1);

Third, for something used often like the call AddForce on the Rigidbody2D of the arrow you should better create a private variable rigidbody that you instantiate in the Start() function from the arrow game object and use that in the shoot() function instead.

Greets,

Jan

in JS "

  • yield WaitForSeconds(1);" works just fine- besides ints not in function FixedUpdate. it is inside shoot();

and after some tweaking it started to work

function shoot(){
if(ready_to_shoot){ready_to_shoot=false;
                       
                         var arrr= Instantiate(trol_arrow, miejste_strzaly.position, miejste_strzaly.rotation );
                         arrr.GetComponent.<Rigidbody2D>().AddForce(miejste_strzaly.forward*500);
yield WaitForSeconds(1);
ready_to_shoot=true;

}}

thx for you intrest :slight_smile: