When do components of loaded prefabs get initialized?

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() {
	
}
}

Hi there

I think the exact issue with your code is that you aren’t instantiating your ‘weapon’. You have use resources.load to load the ‘prefabs’, but you then need to turn those prefabs into actual game objects for them to start/update etc.

Without knowing exactly what behaviour you’re after it’s hard to write the exact code, but if you wanted each of those weapon prefabs to be instantiated, it’d be something like this:

public GameObject[] installedWeaponPrefabs;
public GameObject[] installedWeapons;


void Awake() {
     installedWeaponPrefabs = Resources.LoadAll<GameObject>(resourcesPath);
	 installedWeapons = new GameObject[installedWeaponPrefabs.Length];
	 for(int i = 0; i < installedWeapons.Length; i++)
	 {
		installedWeapons _= Instantiate(installedWeaponPrefabs*);*_

* }*

currWeapon = installedWeapons[currWeapon_index].GetComponent();
}
That code loads all the prefabs, then creates 1 game object from each one and stores it in the installedWeapons array.
If you do ever want to know about when / how the various unity events are fired, this page is very useful:
https://docs.unity3d.com/Manual/ExecutionOrder.html*_
As I say though, I don’t think ordering is the issue here
Hope that helps
_
-Chris*