Prefab not loading in data from Inspector

Okay. What I’m trying to do is create an array of prefabs, but I’m having trouble properly creating them and placing them in the array. I’ve tried several things so far, but these two actually compile and don’t crash immediately:

// 1. An array of GameObjects
public GameObject[] Weapons;
// Setting array size
Weapons = new GameObject[(int)WeaponBase.WeaponType.NumTypes];
Weapons[0] = GameObject.Find("MachineGun");

// 2. An array of base-class scripts
public WeaponBase[] Weapons;
// Setting array size
Weapons = new WeaponBase[(int)WeaponBase.WeaponType.NumTypes];
Weapons[0] = gameObject.AddComponent<WeaponBase>();

Neither of these options assigns the values from the prefab’s Inspector upon creation, but the objects do get created, and are assigned the values within their ctor. I used to be able to Instantiate this prefab before I implemented the array, but since that’s not working, nothing else is.

Any help would be appreciated. Thanks!

Your Weapons array is immedialty overriden on the run by this line:

Weapons = new GameObject[(int)WeaponBase.WeaponType.NumTypes];

Which is creating a new array…
What you need to do is:

public GameObject[] Weapons = new GameObject[(int)WeaponBase.WeaponType.NumTypes]; 
// in the declaration you can set the size without override what is in inspector

As I’m not sure it would compile because of the (int)WeaponBase.WeaponType.NumTypes not being accesible in the declaration (I honestly don’t know) the 2 alternatives you have are:

  • Set the size in script like this: public GameObject Weapons = new GameObject[12];
  • Set the size manually in inspector