Help creating code to change bullet type based on current weapon held

Hello! I am new to this so bear with me, I am creating a game for class where I have 3 different weapons and I am creating 3 different bullets for each weapon. The weapons are different shapes, so a SphereGun, CubeGun, and TriangleGun, so each bullet is a different shape. I was able to create a weapon switching system which allows the player to change weapon using scrollwheel up and down, basically enabling the gameObject if it = the index number. However I have bullet shooting attached to my PlayerMovement script where it checks for if the fire key is being pressed and if so will shoot the weapon. I am wondering if there if a way, maybe using if else statements to check if the gameObject = a certain type and if so then instantiate that bullet type prefab? Basically asking if there is a way to change bullet type based on the weapon being held. I will attach my code below.
this is my WeaponSwitching Code

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 previousthing = 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 (previousthing != selectedWeapon)
        {
            SelectWeapon();
        }
    }
    void SelectWeapon()
    {
        int i = 0;
        foreach (Transform weapon in transform)
        {
            if (i == selectedWeapon)
                weapon.gameObject.SetActive(true);
            else
                weapon.gameObject.SetActive(false);
            i++;
        }
    }
}

Here is the bullet shooting aspect of my PlayerMovement script

void Update()
{
if (Input.GetKeyDown("space"))
{
shoot();
}
}
void shoot()
{
GameObject g = Instantiate(bulletsph, shotSpawn.position, shotSpawn.rotation)
as GameObject;
Destroy(g, 1.5f);
}

I am also attaching screenshots of my project hierarchy layout.

Thanks for the help in advance. Please let me know if you have any questions that will help me to find a soluton :slight_smile:

The general way this is handled is to make “some kind of thing” that has a script that gives you:

  • reference to the prefab for the weapon, which knows how to fire itself
  • reference to the bullet that the firing mechanism uses

“some kind of thing” could be a prefab for the weapon, it could be visible, or it could just be an invisible thing in the scene that offers up what you need, pairing the gun with the weapon.

Or… “some kind of thing” could be a ScriptableObject that you pass into a single generic weapon handler.

Either way, go check out some tutorials on weapon switching to see what I mean. There’s many approaches.

I have the weapon switching part working, although I need each weapon type to fire a different type of bullet, which I have different prefabs of.

Could I make it into a script and apply it to my WeaponHolder group object? Or should I just make individual scripts per each weapon type?