C# static GameObject array in another script

I’ve got an error when im using like that.

public static GameObject[ ] 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”)
};

But when i use it in Start() or Awake() functions it says that weapons are “null” in another class.

try something like this:

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.

Thank you! I’ve got it! I will test it right now.

Well didn’t working…

weapons = Constants.GetWeapons();
if (weapons == null) { Debug.Log(“Weapons are null”); return; }

output “Weapons are null”

I created a couple of prefabs (and filled them in), then put both of these scripts on my camera… I get the answer of 2 in the debug log. It does work.

using UnityEngine;
using System.Collections;

public class Constants : MonoBehaviour {

	public static GameObject[] weapons = new GameObject[0];
    
    void Start(){
        weapons = new GameObject[]
        {
            (GameObject)Resources.Load("Test1"),
            (GameObject)Resources.Load("Test2")
        };
    }

    public static GameObject[] GetWeapons() { return weapons; }
    public static int GetWeaponCount() { return weapons.Length; }
    public static GameObject GetWeapons(int i) {
        if(i < 0 || i > weapons.Length) return null;
        return weapons[i];
    }
}
using UnityEngine;
using System.Collections;


public class Test : MonoBehaviour
{
    void Start() {
        Debug.Log(Constants.GetWeaponCount());
    }
}
1 Like

I have found a soluton!

public static class Constants
{

    public static GameObject[] weapons;
   

    public static GameObject[] GetWeapons() {
        GameObject[] 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")
            };
     return weapons; }

}

WeaponSelect.cs

public void SetDefaultWeapons()
    {

        weapons = Constants.GetWeapons();

        if (weapons == null) { Debug.Log("Weapons are null"); return; }
}

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;

right, in mine, I force the array to a new empty array. So you never get back null.

Hmmm… does it right to do it like that? Never get null but never find where is the bug? I’m comfused a little. Anyway thank you for your help!

That long list of hardcoded .Load calls is making me twitch. Have you considered:

GameObject[] weapons = (GameObject[])Resources.LoadAll("Weapons", typeof(GameObject));

He may have wanted it in a specific order. :wink:

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…(((

Mine working example is a total mess. Every time i need size or one member of an array i need to assign it again…

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 :slight_smile:

I’m new to game development and programming so I’m learning some things everytime.)) Just didn’t knew that before.

I have found a mistake!

weapons = new GameObject[]
            {

            };

// instead of

GameObject[] weapons = new GameObject[]
            {

            };