Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Open inspector causing array change

Discussion in 'Editor & General Support' started by unity_edOjmOMsyZChOA, Feb 18, 2021.

  1. unity_edOjmOMsyZChOA

    unity_edOjmOMsyZChOA

    Joined:
    Sep 22, 2020
    Posts:
    7
    Hi. I ran into an interesting problem today. I am rather new to Unity and C#. All help is welcome.
    I have an array of a custom type that stores a container for my characters armor items. Currently it only stores a container with a helmet item inside on index 0. The rest of the array is null. To update the inventory ui, i loop over the array. Everything is fine untill i inspect the gameobject that has the script with the array atached to it. Now, all of sudden, the previously allmost empty armor array fills itself with empty containers and the inventory ui script runs into a null reference exeption. I do not use any custom editor scripts. Has someone an idea why this could happen?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    is this happening during play mode? During edit mode?

    Do you have an OnValidate() method?
    Are you using [ExecuteAlways] or [ExecuteInEditMode]?

    Share some code!
     
  3. unity_edOjmOMsyZChOA

    unity_edOjmOMsyZChOA

    Joined:
    Sep 22, 2020
    Posts:
    7
    I never used a OnValidate() methode or [ExecuteAlways] / [ExecuteInEditMode]. The exception occures at runtime once i set the gameObject with the CharacterInventoryDisplay(second script) script to SetActive(true) while it is open in the inspector. The null reference exeption guides me back to the SetInventorySlot methode of a script because apassed variable is not set.

    On the second script you can see where the armorPieces array is comming from.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterInventoryDisplay : MonoBehaviour
    6. {
    7.     public CharacterInventory characterInventory;
    8.     public GameObject itemSlotPrefab;
    9.  
    10.     private List<GameObject> armorInventoryObjects = new List<GameObject>();
    11.     private List<GameObject> weaponInventoryObjects = new List<GameObject>();
    12.     private List<GameObject> runeInventoryObjects = new List<GameObject>();
    13.  
    14.  
    15.  
    16.     public void RefreshCharacterInventory(CharacterInventory _caracterInventory)
    17.     {
    18.         characterInventory = _caracterInventory;
    19.         SetInventorySlots();
    20.     }
    21.  
    22.     private void SetInventorySlots()
    23.     {
    24.         WipeInventory();
    25.  
    26.         // armor set
    27.         for (int i = 0; i < characterInventory.armorPieces.Length; i++)
    28.         {
    29.             bool slotHasArmorItem = characterInventory.armorPieces[i] != null;
    30.  
    31.             InventorySlot inventorySlot = armorInventoryObjects[i].GetComponent<InventorySlot>();
    32.             inventorySlot.inventoryType = InventoryType.characterInventory;
    33.  
    34.             if (slotHasArmorItem)
    35.             {
    36.                 InventoryItem inventoryItem = characterInventory.armorPieces[i];
    37.                 inventorySlot.SetInventorySlot(inventoryItem.item, inventoryItem.ammount, characterInventory);
    38.             }
    39.             else
    40.             {
    41.                 inventorySlot.ClearInventorySlot(characterInventory);
    42.             }
    43.         }
    44.  
    45.         // weapon set
    46.         for (int i = 0; i < characterInventory.weapons.Length; i++)
    47.         {
    48.             bool slotHasWeaponItem = characterInventory.weapons[i] != null;
    49.  
    50.             InventorySlot inventorySlot = weaponInventoryObjects[i].GetComponent<InventorySlot>();
    51.             inventorySlot.inventoryType = InventoryType.characterInventory;
    52.  
    53.             if (slotHasWeaponItem)
    54.             {
    55.                 InventoryItem inventoryItem = characterInventory.weapons[i];
    56.                 inventorySlot.SetInventorySlot(inventoryItem.item, inventoryItem.ammount, characterInventory);
    57.             }
    58.             else
    59.             {
    60.                 inventorySlot.ClearInventorySlot(characterInventory);
    61.             }
    62.         }
    63.  
    64.         // rune set
    65.         for (int i = 0; i < characterInventory.runes.Length; i++)
    66.         {
    67.             bool slotHasRuneItem = characterInventory.runes[i] != null;
    68.  
    69.             InventorySlot inventorySlot = runeInventoryObjects[i].GetComponent<InventorySlot>();
    70.             inventorySlot.inventoryType = InventoryType.characterInventory;
    71.  
    72.             if (slotHasRuneItem)
    73.             {
    74.                 InventoryItem inventoryItem = characterInventory.runes[i];
    75.                 inventorySlot.SetInventorySlot(inventoryItem.item, inventoryItem.ammount, characterInventory);
    76.             }
    77.             else
    78.             {
    79.                 inventorySlot.ClearInventorySlot(characterInventory);
    80.             }
    81.         }
    82.     }
    83.  
    84.     private void WipeInventory()
    85.     {
    86.         armorInventoryObjects = new List<GameObject>();
    87.         WipeRunes();
    88.  
    89.         for (int i = 0; i < transform.childCount; i++)
    90.         {
    91.             GameObject itemSlot = transform.GetChild(i).gameObject;
    92.             InventorySlot inventorySlot = itemSlot.GetComponent<InventorySlot>();
    93.  
    94.             if (inventorySlot != null)
    95.             {
    96.                 if (i < characterInventory.armorPiecesAmmount + 1)
    97.                 {
    98.                     armorInventoryObjects.Add(itemSlot);
    99.                 }
    100.                 else if (i < characterInventory.armorPiecesAmmount + characterInventory.weaponsAmmount + 1)
    101.                 {
    102.                     weaponInventoryObjects.Add(itemSlot);
    103.                 }
    104.  
    105.                 inventorySlot.ClearInventorySlot(characterInventory);
    106.             }
    107.         }
    108.     }
    109.  
    110.     private void WipeRunes()
    111.     {
    112.         Transform runeSlotContainerTr = transform.GetChild(0);
    113.  
    114.         for (int j = 0; j < runeSlotContainerTr.childCount; j++)
    115.         {
    116.             Destroy(runeSlotContainerTr.GetChild(j).gameObject);
    117.         }
    118.  
    119.         for (int j = 0; j < characterInventory.runes.Length; j++)
    120.         {
    121.             GameObject newRuneSlot = Instantiate(itemSlotPrefab, runeSlotContainerTr);
    122.             runeInventoryObjects.Add(newRuneSlot);
    123.             newRuneSlot.GetComponent<InventorySlot>().ClearInventorySlot(characterInventory);
    124.         }
    125.     }
    126. }



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(StatsController))]
    6. public class CharacterInventory : ItemInventory
    7. {
    8.     private StatsController statsController;
    9.  
    10.     public InventoryItem[] armorPieces;
    11.     public InventoryItem[] weapons;
    12.     public InventoryItem[] runes;
    13.  
    14.     [HideInInspector]
    15.     public int armorPiecesAmmount;
    16.     [HideInInspector]
    17.     public int weaponsAmmount = 2;
    18.     [HideInInspector]
    19.     public int runeAmmount;
    20.  
    21.     private new void Awake()
    22.     {
    23.         GameEvents.current.onUiRefresh += ResetSlots;
    24.         base.Awake();
    25.         inventoryType = InventoryType.characterInventory;
    26.         statsController = GetComponent<StatsController>();
    27.     }
    28.  
    29.     public void ResetSlots()
    30.     {
    31.         InventoryItem[] tempArmorPieces = armorPieces;
    32.         InventoryItem[] tempWeapons = weapons;
    33.         InventoryItem[] tempRunes = runes;
    34.  
    35.         armorPiecesAmmount = System.Enum.GetNames(typeof(ArmorType)).Length;
    36.         runeAmmount = statsController.runeSlots;
    37.  
    38.         armorPieces = new InventoryItem[armorPiecesAmmount];
    39.         weapons = new InventoryItem[weaponsAmmount];
    40.         runes = new InventoryItem[runeAmmount];
    41.  
    42.         for (int i = 0; i < tempArmorPieces.Length; i++)
    43.         {
    44.             armorPieces[i] = tempArmorPieces[i];
    45.         }
    46.  
    47.         for (int i = 0; i < tempWeapons.Length; i++)
    48.         {
    49.             weapons[i] = tempWeapons[i];
    50.         }
    51.  
    52.         for (int i = 0; i < tempRunes.Length; i++)
    53.         {
    54.             runes[i] = tempRunes[i];
    55.         }
    56.     }
    57.  
    58.     public void EquipItem(Item _item)
    59.     {
    60.         if (_item.armor != null)
    61.         {
    62.             if (armorPieces[(int)_item.armor.armorType] == null)
    63.             {
    64.                 TryMoveItemInInventory(_item, -1);
    65.                 armorPieces[(int)_item.armor.armorType] = new InventoryItem(_item, 1);
    66.             }
    67.         }
    68.         else if (_item.weapon != null)
    69.         {
    70.             if (weapons[(int)_item.armor.armorType] == null)
    71.             {
    72.                 TryMoveItemInInventory(_item, -1);
    73.                 weapons[0] = new InventoryItem(_item, 1);
    74.             }
    75.         }
    76.         else if (_item.rune != null)
    77.         {
    78.             for (int i = 0; i < runes.Length; i++)
    79.             {
    80.                 if (runes[i] == null)
    81.                 {
    82.                     TryMoveItemInInventory(_item, -1);
    83.                     runes[i] = new InventoryItem(_item, 1);
    84.                     break;
    85.                 }
    86.             }
    87.         }
    88.         else
    89.         {
    90.             Debug.LogError("Tried to equip non equippeable.");
    91.         }
    92.     }
    93.  
    94.     public void UnEquipItem(Item _item)
    95.     {
    96.         if (_item.armor != null)
    97.         {
    98.             for (int i = 0; i < armorPiecesAmmount; i++)
    99.             {
    100.                 if (armorPieces[i] != null)
    101.                 {
    102.                     ItemArmor item = armorPieces[i].item.armor;
    103.  
    104.                     if (item == _item)
    105.                     {
    106.                         TryMoveItemInInventory(item, 1);
    107.                         armorPieces[i] = null;
    108.                         break;
    109.                     }
    110.                 }
    111.             }
    112.         }
    113.         else if (_item.weapon != null)
    114.         {
    115.             for (int i = 0; i < weaponsAmmount; i++)
    116.             {
    117.                 if (weapons[i] != null)
    118.                 {
    119.                     ItemWeapon item = weapons[i].item.weapon;
    120.  
    121.                     if (item == _item)
    122.                     {
    123.                         TryMoveItemInInventory(item, 1);
    124.                         weapons[i] = null;
    125.                         break;
    126.                     }
    127.                 }
    128.             }
    129.         }
    130.         else if (_item.rune != null)
    131.         {
    132.             for (int i = 0; i < runeAmmount; i++)
    133.             {
    134.                 if (runes[i] != null)
    135.                 {
    136.                     ItemRune item = runes[i].item.rune;
    137.  
    138.                     if (item == _item)
    139.                     {
    140.                         TryMoveItemInInventory(item, 1);
    141.                         runes[i] = null;
    142.                         break;
    143.                     }
    144.                 }
    145.             }
    146.         }
    147.      
    148.     }
    149.  
    150.     private void OnDestroy()
    151.     {
    152.         GameEvents.current.onUiRefresh -= ResetSlots;
    153.     }
    154. }