Search Unity

C# static GameObject array in another script

Discussion in 'Scripting' started by radokhlebov, Jan 19, 2016.

  1. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    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.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    try something like this:
    Code (csharp):
    1.  
    2. public static GameObject[] weapons = new GameObject[];
    3.    
    4.     void Start(){
    5.         weapons = new GameObject[]
    6.         {
    7.             (GameObject)Resources.Load("Weapons/AK47GOLD"),
    8.             (GameObject)Resources.Load("Weapons/AK47U"),
    9.             (GameObject)Resources.Load("Weapons/AK74"),
    10.             (GameObject)Resources.Load("Weapons/FNFAL"),
    11.             (GameObject)Resources.Load("Weapons/FNminimi"), //5
    12.             (GameObject)Resources.Load("Weapons/FNSCAR"),
    13.             (GameObject)Resources.Load("Weapons/M4A1"),
    14.             (GameObject)Resources.Load("Weapons/M4A1attach"),
    15.             (GameObject)Resources.Load("Weapons/MAC10"),
    16.             (GameObject)Resources.Load("Weapons/MOSINNagant"), //10
    17.             (GameObject)Resources.Load("Weapons/UZI")
    18.         };
    19.     }
    20.  
    21.     public static GameObject[] GetWeapons() { return weapons; }
    22.     public static GameObject[] GetWeaponCount() { return weapons.Length; }
    23.     public static GameObject GetWeapon(int i) {
    24.         if(i < 0 || i > weapons.Length) return null;
    25.         return weapons[i];
    26.     }
    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.
     
  3. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    Thank you! I've got it! I will test it right now.
     
  4. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    Well didn't working...

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

    output "Weapons are null"
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Constants : MonoBehaviour {
    6.  
    7.     public static GameObject[] weapons = new GameObject[0];
    8.    
    9.     void Start(){
    10.         weapons = new GameObject[]
    11.         {
    12.             (GameObject)Resources.Load("Test1"),
    13.             (GameObject)Resources.Load("Test2")
    14.         };
    15.     }
    16.  
    17.     public static GameObject[] GetWeapons() { return weapons; }
    18.     public static int GetWeaponCount() { return weapons.Length; }
    19.     public static GameObject GetWeapons(int i) {
    20.         if(i < 0 || i > weapons.Length) return null;
    21.         return weapons[i];
    22.     }
    23. }
    24.  
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. public class Test : MonoBehaviour
    7. {
    8.     void Start() {
    9.         Debug.Log(Constants.GetWeaponCount());
    10.     }
    11. }
    12.  
     
    radokhlebov likes this.
  6. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    I have found a soluton!

    Code (CSharp):
    1. public static class Constants
    2. {
    3.  
    4.     public static GameObject[] weapons;
    5.    
    6.  
    7.     public static GameObject[] GetWeapons() {
    8.         GameObject[] weapons = new GameObject[]
    9.             {
    10.                 (GameObject)Resources.Load("Weapons/AK47GOLD"),
    11.                 (GameObject)Resources.Load("Weapons/AK47U"),
    12.                 (GameObject)Resources.Load("Weapons/AK74"),
    13.                 (GameObject)Resources.Load("Weapons/FNFAL"),
    14.                 (GameObject)Resources.Load("Weapons/FNminimi"), //5
    15.                 (GameObject)Resources.Load("Weapons/FNSCAR"),
    16.                 (GameObject)Resources.Load("Weapons/M4A1"),
    17.                 (GameObject)Resources.Load("Weapons/M4A1attach"),
    18.                 (GameObject)Resources.Load("Weapons/MAC10"),
    19.                 (GameObject)Resources.Load("Weapons/MOSINNagant"), //10
    20.                 (GameObject)Resources.Load("Weapons/UZI")
    21.             };
    22.      return weapons; }
    23.  
    24. }
    WeaponSelect.cs
    Code (CSharp):
    1. public void SetDefaultWeapons()
    2.     {
    3.  
    4.         weapons = Constants.GetWeapons();
    5.  
    6.         if (weapons == null) { Debug.Log("Weapons are null"); return; }
    7. }
     
  7. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    Well strange things are happening... mine was like yours but corrected one mistake and when i was declearing array it was like
    Code (CSharp):
    1. public static GameObject[] weapons;
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    right, in mine, I force the array to a new empty array. So you never get back null.
     
  9. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    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!
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That long list of hardcoded .Load calls is making me twitch. Have you considered:
    Code (csharp):
    1. GameObject[] weapons = (GameObject[])Resources.LoadAll("Weapons", typeof(GameObject));
     
  11. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    He may have wanted it in a specific order. ;)
     
  12. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    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...(((
     
  13. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    Mine working example is a total mess. Every time i need size or one member of an array i need to assign it again...
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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 :)
     
  15. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    I'm new to game development and programming so I'm learning some things everytime.)) Just didn't knew that before.
     
  16. radokhlebov

    radokhlebov

    Joined:
    Mar 13, 2015
    Posts:
    14
    I have found a mistake!
    Code (CSharp):
    1. weapons = new GameObject[]
    2.             {
    3.  
    4.             };
    5.  
    6. // instead of
    7.  
    8. GameObject[] weapons = new GameObject[]
    9.             {
    10.  
    11.             };