I have been following Brackey’s tutorial on weapon switching to apply it to B Mo’s 5-minute top down shooter. Yet when I enter playmode, neither of my weapons are visible but the cannon can still shoot. All of my relevant script used are below.
WeaponSwitcher:
using UnityEngine;
public class WeaponSwitching : MonoBehaviour
{
public int selectedWeapon = 0;
// Start is called before the first frame update
void Start()
{
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
selectedWeapon = 0;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
{
selectedWeapon = 1;
}
if (previousSelectedWeapon != selectedWeapon)
{
SelectWeapon();
}
}
void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
weapon.gameObject.SetActive(false);
i++;
}
}
}
Cannon:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cannon : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform FirePoint;
public float fireForce = 50f;
public int currentClip, maxClipSize = 50, currentAmmo, maxAmmoSize = 500;
public void Fire()
{
if (currentClip > 0)
{
GameObject bullet = Instantiate(bulletPrefab, FirePoint.position, FirePoint.rotation);
bullet.GetComponent<Rigidbody2D>().AddForce(FirePoint.up * fireForce, ForceMode2D.Impulse);
currentClip--;
}
}
public void Reload()
{
int reloadAmount = maxClipSize - currentClip;
reloadAmount = (currentAmmo - reloadAmount) >= 0 ? reloadAmount : currentAmmo;
currentClip += reloadAmount;
currentAmmo -= reloadAmount;
}
public void AddAmmo(int ammoAmount)
{
currentAmmo += ammoAmount;
if (currentAmmo > maxAmmoSize)
{
currentAmmo = maxAmmoSize;
}
}
}
Bullet:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.TryGetComponent<EnemyHealth>(out EnemyHealth enemyComponent))
{
enemyComponent.TakeDamage(1);
}
Destroy(gameObject);
}
}
Weapon (1):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform FirePoint;
public float fireForce = 20f;
public int currentClip, maxClipSize = 10, currentAmmo, maxAmmoSize = 100;
public void Fire()
{
if (currentClip > 0)
{
GameObject bullet = Instantiate(bulletPrefab, FirePoint.position, FirePoint.rotation);
bullet.GetComponent<Rigidbody2D>().AddForce(FirePoint.up * fireForce, ForceMode2D.Impulse);
currentClip--;
}
}
public void Reload()
{
int reloadAmount = maxClipSize - currentClip;
reloadAmount = (currentAmmo - reloadAmount) >= 0 ? reloadAmount : currentAmmo;
currentClip += reloadAmount;
currentAmmo -= reloadAmount;
}
public void AddAmmo(int ammoAmount)
{
currentAmmo += ammoAmount;
if(currentAmmo > maxAmmoSize)
{
currentAmmo = maxAmmoSize;
}
}
}
Player Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Weapon Weapon;
public Cannon Cannon;
Vector2 moveDirection;
Vector2 mousePosition;
void Start()
{
}
void Update()
{
float moveX = Input.GetAxisRaw(“Horizontal”);
float moveY = Input.GetAxisRaw(“Vertical”);
if(Input.GetMouseButtonDown(0))
{
Weapon.Fire();
}
if (Input.GetMouseButtonDown(0))
{
Cannon.Fire();
}
if (Input.GetKeyDown(KeyCode.R))
{
Weapon.Reload();
}
if (Input.GetKeyDown(KeyCode.R))
{
Cannon.Reload();
}
moveDirection = new Vector2(moveX, moveY).normalized;
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void FixedUpdate()
{
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed);
Vector2 aimDirection = mousePosition - rb.position;
float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = aimAngle;
}
}