Hello, I’m trying to get a weapon array to work so I can have my player shoot different prefabs. If I keep the public field as one prefab it works fine, I tried to add the array and it’s not working. Can someone point me in the right direction? Thanks (and by the way I realize there is currently nothing to change the currentWeapon int from 0 to 1, but I’ve been doing it manually to test it)!
// Launch projectile
public class PlayerWeaponScript : MonoBehaviour
{
public Transform [] shotPrefab;// Projectile prefab for shooting
public float shootingRate = 0.25f;// Cooldown in seconds between two shots
private float shootCooldown;
private int currentWeapon = 0;
void Start()
{
shootCooldown = 0f;
}
void Update()
{
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
//Shooting from another script
// Create a new projectile if possible
public void Attack(bool isEnemy) {
if (CanAttack) {
if (currentWeapon == 0) {
int shotIndex = 0;
} else if (currentWeapon == 1) {
int shotIndex = 1;
shootCooldown = shootingRate;
var shotTransform = Instantiate (shotPrefab [shotIndex]) as Transform;// Create a new shot
shotTransform.position = transform.position;// Assign position
ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript> ();
if (shot != null) {
shot.isEnemyShot = isEnemy;
gameObject.tag = "Enemy";
}
// Make the weapon shot always toward
EnemyMoveScript move = shotTransform.gameObject.GetComponent<EnemyMoveScript> ();
if (move != null) {
move.direction = this.transform.right; // towards in 2D space is the right of the sprite
}
}
}
}
public bool CanAttack// Is the weapon ready to create a new projectile?
{
get
{
return shootCooldown <= 0f;
}
}
}