I’ve made this script to control my players weapons, inventory and pickups. I don’t want to use multiple scripts for each because plan on adding coop and I want it to be modular. I’m not getting any errors and it seems to work fine other than the pickups, weapon selection and every time the game starts it makes the gravity gun model visible but have the effects of the rifle.
Code:
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Runtime.CompilerServices;
using Unity.Entities;
using Unity.VisualScripting;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;
public class PlayerManager : MonoBehaviour
{
[Header("HUD")]
public GameObject mainWeapon;
public GameObject sidearmWeapon;
public GameObject utilityItem;
public GameObject pickupText;
[Header("Abilities")]
public GameObject Dash;
public GameObject Hack;
[Header("Inventory")]
public GameObject inventory;
public GameObject helmet;
public GameObject boots;
public GameObject chestpiece;
[Header("Weapons")]
public GameObject Rifle;
public GameObject Pistol;
public GameObject Minigun;
public GameObject Sword;
public GameObject ToxinLauncher;
public GameObject GravityGuantlet;
public GameObject RailGunPistol;
[Header("Gun Script")]
public GameObject GunScript;
public Prefab standardLaser;
public Prefab gravityLaser;
public Prefab toxinLaser;
public Prefab projectile;
[Header("Player Stats")]
public float Health = 100f;
public float Speed = 11.5f;
public float runSpeed = 15.5f;
public float jumpForce = 7f;
public float Damage = 10f;
[Header("Utility Items")]
public GameObject impluseGrenade;
public GameObject repairKit;
public GameObject advancedRepairKit;
public GameObject grapple;
public GameObject impactGrenade;
public GameObject empGrenade;
public GameObject jetPack;
// private bools
private bool curMWep;
private bool curSWep;
private bool selectedWeapon;
private bool curUtil;
private bool mwepchange;
// Start
void Start()
{
curMWep = Rifle;
curSWep = Pistol;
pickupText.SetActive(false);
selectedWeapon = Rifle;
Rifle.SetActive(true);
}
// Update
void Update()
{
WeaponSelector();
selectWeapons();
}
// Inventory
public void Inventory()
{
if (Input.GetKeyDown(KeyCode.Tab) && inventory != null)
{
inventory.SetActive(true);
}
}
// Abilities
public void Ability()
{
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Dash was used.");
}
if (Input.GetKeyDown(KeyCode.Q))
{
Debug.Log("Hack was used.");
}
}
private void OnTriggerEnter(Collider other)
{
pickupText.SetActive(true);
if (other.CompareTag("RiflePickup") && Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Player picked up rifle!");
Destroy(other);
curMWep = Rifle;
}
if (other.CompareTag("PistolPickup") && Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Player picked up Pistol!");
Destroy(other);
curMWep = Pistol;
}
if (other.CompareTag("MinigunPickup") && Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Player picked up Minigun!");
Destroy(other);
curMWep = Minigun;
}
if (other.CompareTag("SwordPickup") && Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Player picked up Sword!");
Destroy(other);
curMWep = Sword;
}
if (other.CompareTag("RailgunPickup") && Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Player picked up Railgun!");
Destroy(other);
curMWep = RailGunPistol;
}
if (other.CompareTag("ToxinPickup") && Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Player picked up Toxin Launcher!");
Destroy(other);
curMWep = ToxinLauncher;
}
if (other.CompareTag("GraivtyGunPickup") && Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Player picked up Gravity Guantlet!");
Destroy(other);
curMWep = GravityGuantlet;
}
// Side Arm Weapon Pickups
if (other.CompareTag("RiflePickup") && Input.GetKeyDown(KeyCode.G))
{
Debug.Log("Player picked up rifle!");
Destroy(other);
curSWep = Rifle;
}
if (other.CompareTag("PistolPickup") && Input.GetKeyDown(KeyCode.G))
{
Debug.Log("Player picked up Pistol!");
Destroy(other);
curSWep = Pistol;
}
if (other.CompareTag("MinigunPickup") && Input.GetKeyDown(KeyCode.G))
{
Debug.Log("Player picked up Minigun!");
Destroy(other);
curSWep = Minigun;
}
if (other.CompareTag("SwordPickup") && Input.GetKeyDown(KeyCode.G))
{
Debug.Log("Player picked up Sword!");
Destroy(other);
curSWep = Sword;
}
if (other.CompareTag("RailgunPickup") && Input.GetKeyDown(KeyCode.G))
{
Debug.Log("Player picked up Railgun!");
Destroy(other);
curSWep = RailGunPistol;
}
if (other.CompareTag("ToxinPickup") && Input.GetKeyDown(KeyCode.G))
{
Debug.Log("Player picked up Toxin Launcher!");
Destroy(other);
curSWep = ToxinLauncher;
}
if (other.CompareTag("GraivtyGunPickup") && Input.GetKeyDown(KeyCode.G))
{
Debug.Log("Player picked up Gravity Guantlet!");
Destroy(other);
curSWep = GravityGuantlet;
}
}
public void WeaponSelector()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
selectedWeapon = curMWep;
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
selectedWeapon = curSWep;
}
if (Input.GetKeyDown(KeyCode.R))
{
UseUtility();
}
}
public void selectWeapons()
{
if (selectedWeapon == Rifle)
{
Rifle.SetActive(true);
Pistol.SetActive(false);
Minigun.SetActive(false);
ToxinLauncher.SetActive(false);
Sword.SetActive(false);
RailGunPistol.SetActive(false);
GravityGuantlet.SetActive(false);
}
if (selectedWeapon == Pistol)
{
Rifle.SetActive(false);
Pistol.SetActive(true);
Minigun.SetActive(false);
ToxinLauncher.SetActive(false);
Sword.SetActive(false);
RailGunPistol.SetActive(false);
GravityGuantlet.SetActive(false);
}
if (selectedWeapon == Minigun)
{
Rifle.SetActive(false);
Pistol.SetActive(false);
Minigun.SetActive(true);
ToxinLauncher.SetActive(false);
Sword.SetActive(false);
RailGunPistol.SetActive(false);
GravityGuantlet.SetActive(false);
}
if (selectedWeapon == ToxinLauncher)
{
Rifle.SetActive(false);
Pistol.SetActive(false);
Minigun.SetActive(false);
ToxinLauncher.SetActive(true);
Sword.SetActive(false);
RailGunPistol.SetActive(false);
GravityGuantlet.SetActive(false);
}
if (selectedWeapon == Sword)
{
Rifle.SetActive(false);
Pistol.SetActive(false);
Minigun.SetActive(false);
ToxinLauncher.SetActive(false);
Sword.SetActive(true);
RailGunPistol.SetActive(false);
GravityGuantlet.SetActive(false);
}
if (selectedWeapon == RailGunPistol)
{
Rifle.SetActive(false);
Pistol.SetActive(false);
Minigun.SetActive(false);
ToxinLauncher.SetActive(false);
Sword.SetActive(false);
RailGunPistol.SetActive(true);
GravityGuantlet.SetActive(false);
}
if (selectedWeapon == GravityGuantlet)
{
Rifle.SetActive(false);
Pistol.SetActive(false);
Minigun.SetActive(false);
ToxinLauncher.SetActive(false);
Sword.SetActive(false);
RailGunPistol.SetActive(false);
GravityGuantlet.SetActive(true);
}
}
public void UseUtility()
{
if (utilityItem == impactGrenade)
{
}
if (utilityItem == impluseGrenade)
{
}
if (utilityItem == repairKit)
{
}
if (utilityItem == advancedRepairKit)
{
}
if (utilityItem == empGrenade)
{
}
if (utilityItem == jetPack)
{
}
}
}