public static GameObject[] weapons = new GameObject[];
void Start(){
weapons = new GameObject[]
{
(GameObject)Resources.Load("Weapons/AK47GOLD"),
(GameObject)Resources.Load("Weapons/AK47U"),
(GameObject)Resources.Load("Weapons/AK74"),
(GameObject)Resources.Load("Weapons/FNFAL"),
(GameObject)Resources.Load("Weapons/FNminimi"), //5
(GameObject)Resources.Load("Weapons/FNSCAR"),
(GameObject)Resources.Load("Weapons/M4A1"),
(GameObject)Resources.Load("Weapons/M4A1attach"),
(GameObject)Resources.Load("Weapons/MAC10"),
(GameObject)Resources.Load("Weapons/MOSINNagant"), //10
(GameObject)Resources.Load("Weapons/UZI")
};
}
public static GameObject[] GetWeapons() { return weapons; }
public static GameObject[] GetWeaponCount() { return weapons.Length; }
public static GameObject GetWeapon(int i) {
if(i < 0 || i > weapons.Length) return null;
return weapons[i];
}
I didn’t test it, but it is a little different. You would have to have. The concept is that you put this on an object in the scene and it loads the weapons. Then you access it as a singleton.
Well strange things are happening… mine was like yours but corrected one mistake and when i was declearing array it was like public static GameObject[] weapons;
Thank you) but there is weapons like pistols and a landmine in the same folder. Should i move it out? I’ve tried bigmisterb’s example right now. And STILL it did not worked for me. there is no null error but 0 array size…(((
Well… those are weapons too, right? I would posit that if your intent is to exclude those, then your array is inappropriately named, at least.
If I were making this, I would make sure each weapon has a WeaponProperties (or similar) script; this script knows which ‘class’ of weapon it is (mine, pistol, automatic, whichever), and your static class would have an accessor that loops through the ‘all weapons’ array (made with LoadAll) and makes an array with just the ones you want.
Alternately, put each class in its own subfolder.
Sorry to sidetrack you, I just saw that .LoadAll hadn’t been mentioned and wasn’t sure if you just didn’t know it was an option. Carry on