So I’m making a 2D Space Shooter game, and I’m having my player shoot lots of types of bullets by using Instantiate to clone from a prefab, but the bullet keeps damaging my ship because even though the prefab is set to my “Player” layer (as well as my ship) and Player/Player collision is turned off, the bullet still hits me, which I eventually discovered is because it’s cloning it onto the default layer instead of Player.
Is there any way I can force the bullet to clone onto the correct layer?
My full C# PlayerShooting.cs script (which is on the player) is this, in case it helps:
public class PlayerShooting : MonoBehaviour {
public GameObject bulletPrefab;
public float fireDelay;
float cooldownTimer = 0;
void Update () {
cooldownTimer -= Time.deltaTime;
if (Input.GetButtonDown ("Fire1") && cooldownTimer <= 0) {
// Bullet fires:
gameObject.layer = 10;
Instantiate(bulletPrefab, transform.position, transform.rotation);
gameObject.layer = 8;
Debug.Log("Fired!");
//Cooldown Timer is reset:
cooldownTimer = fireDelay;
}
}
}