- Home /
The question is answered, right answer was accepted
Player Collider isn't found by Physics2D
I am trying to have a bullet fly out my ship. However, the bullet doesn't move and I think I have the error down to the fact, that it collides with my ship.
This is the Firing-Coroutine:
IEnumerator FireGun()
{
GameObject bulletClone = Instantiate(bullet, transform.position, player.GetComponent<Transform>().rotation);
bulletClone.GetComponent<BulletBehavior>().damage = damage;
bulletClone.GetComponent<Rigidbody2D>().AddForce(bulletClone.transform.up * bulletSpeed);
Physics2D.IgnoreLayerCollision(8, 9);
Debug.Log(bulletClone.transform.up * bulletSpeed);
Debug.Log(bulletClone.GetComponent<Rigidbody2D>().velocity);
Debug.Log(Physics2D.GetIgnoreLayerCollision(8, 9));
yield return null;
}
The Player does have the Layer 8, the Bullets do have the Layer 9 and the Log shows those layers get ignored. Log also shows, that a force of 0,500,0 should be applied, but the velocity is 0,0,0.
This is the error: ""MissingComponentException: There is no 'Collider' attached to the "Player" game object, but a script is trying to access it. You probably need to add a Collider to the game object "Player". Or your script needs to check if the component is attached before using it.""
This is my player:
It clearly has a collider.
Does anybody know what is happening here? Thanks very much in advance Jan
Wait, am I blind or you didn't include the code where you try to access the collider?
I do not actually access the player collider directly. I try to Ignore the Collision via the layers. I used to have the following code:
Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), bulletClone.GetComponent<Collider2D>());
Which doesn't work either. "player" is a GameObject with pointing at the player.
However: Wierdly enough the Error $$anonymous$$essage is gone after I switched out the Collision detection again (Layers to colliders).... The bullets still don't move though.
Answer by TheSartremaster · Aug 31, 2018 at 01:20 PM
Okay the problem is gone.
The collider error did just disappear... no clue why. The bullets now fly after I just cranked up the speed. Thanks everyone
Answer by myzzie · Aug 31, 2018 at 01:14 PM
Attach a collider to the player game object
The screenshot is the player game object. There is a collider.
Refering to my comment though: The error message is now miraculously gone after I went back to checking the collision via player and bullet collider directly.
Can't see anything wrong with your script as long as bullet speed is higher than 0 and the bullet's rigidbody is setup correctly. Good luck
Follow this Question
Related Questions
ignoreing enemy collisions when hit, and turning the coliders back on 1 Answer
Collision 2d is not working perfectly 0 Answers
OnTriggerEnter2D triggering multiple times when moving around inside the trigger area. 1 Answer
Set ContactFilter2d to not use child object's triggers. 2 Answers
Adding force at an angle 2D,Adding force at an angle 1 Answer