I have a Resources folder where I store all of my game weapon prefabs. Upon loading the main scene, I store all of those in a list with a Resources.LoadAll call in a WeaponSystem component. Each prefab has a component that derives from a base Weapon class and WeaponSystem swaps between them at runtime, just setting the currentWeapon variable to the new selected weapon.
In my TractorBeam weapon script I’ve placed two print statements, one in Start, and another in Update, neither of them are being called when the game runs. I’m trying to initialize variables and stuff in these scripts but no Monobehaviour derived method wants to run.
public class WeaponSystem : MonoBehaviour {
public GameObject[] installedWeapons;
private Weapon currWeapon;
private int currWeapon_index = 0;
private string resourcesPath = "Prefabs/Weapons/";
private List<Transform> fireLocations = new List<Transform>();
private InputMapper im;
void Awake() {
installedWeapons = Resources.LoadAll<GameObject>(resourcesPath);
currWeapon = installedWeapons[currWeapon_index].GetComponent<Weapon>();
}
// Use this for initialization
void Start () {
im = GameObject.Find("GameManager").GetComponent<InputMapper>();
fireLocations.Add(transform.Find("fire1"));
fireLocations.Add(transform.Find("fire2"));
}
// Update is called once per frame
void Update () {
if (currWeapon != null) {
if (currWeapon.isContinuousFire) {
if (Input.GetKey(im.currentWeaponMainFireKey)) {
ShootPrimary();
}
}
else {
if (Input.GetKeyDown(im.currentWeaponMainFireKey)) {
ShootPrimary();
}
}
}
if (Input.GetKeyDown(im.nextWeaponKey)) {
SetNextWeapon();
}
if (Input.GetKeyDown(im.previousWeaponKey)) {
SetPreviousWeapon();
}
}
private void ShootPrimary() {
foreach (Transform location in fireLocations) {
currWeapon.Fire(location);
}
}
private void SetNextWeapon() {
currWeapon_index = (currWeapon_index+1).Modulo(installedWeapons.Length);
currWeapon = installedWeapons[currWeapon_index].GetComponent<Weapon>();
}
private void SetPreviousWeapon() {
currWeapon_index = (currWeapon_index-1).Modulo(installedWeapons.Length);
currWeapon = installedWeapons[currWeapon_index].GetComponent<Weapon>();
}
void OnGUI() {
GUI.Label(new Rect(10, 150, 300, 20), "Current weapon: " + currWeapon.w_name);
}
}
public class TractorBeam : Weapon {
public float beamRadius = 5;
public float beamReach = 20;
public float beamForce = 2;
public override bool isContinuousFire {
get { return true; }
}
public override string w_name {
get { return "Tractor Beam"; }
}
private PlayerMovement pm;
void Awake () {
print ("started");
}
// Update is called once per frame
void Update () {
print ("in update");
}
public override void Fire(Transform location) {
...
}
}
public class Weapon : MonoBehaviour {
public Dictionary<ResourceType, float> requiredResources = new Dictionary<ResourceType, float>();
public virtual bool isContinuousFire {
get { return false; }
}
public virtual string w_name {
get { return "Unnamed weapon"; }
}
//Fires a weapon at the Transform location
public virtual void Fire(Transform location) {
}
void Start() {
}
void Update() {
}
}