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 Script to reference GameObject not working on Mobile test?

Discussion in 'Scripting' started by JasonZeno, May 10, 2023.

Thread Status:
Not open for further replies.
  1. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    Hey guys,

    I'm working on a small mobile RPG that includes items dropping from enemies. I have a script that pulls the reference of the inventory, which resides on the "PlayerUI" canvas.

    public class FindReferenceOfInventory : MonoBehaviour
    {

    public GameObject inventory;

    void Start()
    {
    inventory = GameObject.Find("PlayerUI");
    }


    }

    This method works as it should in the Unity editor but when I transfer it over for a mobile Android test, it doesn't work at all.

    Am I missing something?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    GameObject.Find works just fine on Android and any platform. Something else is going on and we would need more details than this single script snippet to help you.

    One typical reason you might see differing behavior in a build vs the editor is that you may have code which depends on a certain order of execution. For example maybe this "PlayerUI" object is being instantiated in the Start method of some other script. In that case, it could be that in the editor that other script is running first, and in your build this script is running first. It's important to note that Unity makes no guarantees about script execution order by default.

    And for the future: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    I honestly wouldn't know where to begin, there's so many scipts that run the inventory.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    begin with what you know.

    You said "it doesn't work at all"

    Well first you need to qualify exactly what that means. Start by doing some logging and getting some basic answers:
    • Is the code in question running at all? Use Debug.Log to find out
    • Is the code in question running, but it's not finding the object? Then the object with that name doesn't exist or is not active in the scene at the time that code runs. From here you can investigate the code that spawns the object for example. Is that code running? When is that code running?
    Pretty straightforward.
     
  5. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    I'm going to try to dig a little deeper and I'll share some code that I think is relevant. I've been trying to get my inventory to stay referenced between scenes and I was having a hard time.

    Along the way I made that code above, which I thought was making it work. Then I realized that my inventory reference was remaining only if I changed scenes while it was still open.

    I removed the above code and the reference is still remaining as long as the inventory is open while scene change so it's got to be coming from somewhere else, I'll take a look.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,559
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Don't just post sheets of code and yell "help." Instead, use this process:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post(s) above.

    Just as a heads-up, keep this in mind:

    These things (inventory, shop systems, character customization, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

    Inventory code never lives "all by itself." All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

    Inventories / shop systems / character selectors all contain elements of:

    - a database of items that you may possibly possess / equip
    - a database of the items that you actually possess / equip currently
    - perhaps another database of your "storage" area at home base?
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
    - interaction with items in the inventory or on the character or in the home base storage area
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    Just the design choices of such a system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - if there is an item limit, what is it? Total count? Weight? Size? Something else?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items? How? Limits? Results? Messages of success/failure?
    - can users substantially modify items with other things like spells, gems, sockets, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    If you want to see most of the steps involved, make a "micro inventory" in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

    Everything you learn doing that "micro inventory" of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

    Breaking down large problems in general:

    https://forum.unity.com/threads/opt...n-an-asteroid-belt-game.1395319/#post-8781697
     
  7. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    Code (CSharp):
    1. public class ItemBox : MonoBehaviour
    2. {
    3.     [SerializeField] Item item;
    4.     [SerializeField] int amount = 1;
    5.     [SerializeField] SpriteRenderer spriteRenderer;
    6.     [SerializeField] Color emptyColor;
    7.     [SerializeField] KeyCode itemPickupKeyCode = KeyCode.E;
    8.     [SerializeField] TextMeshProUGUI itemNameText;
    9.     [SerializeField] Button pickUpButton;
    10.  
    11.  
    12.  
    13.    
    14.     public Inventory inventory;
    15.     private bool isInRange;
    16.     private bool isEmpty;
    17.     private bool pickUpAllowed;
    18.  
    19.  
    20.  
    21.     void Start()
    22.     {
    23.         pickUpButton.gameObject.SetActive(false);
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         Button btn = pickUpButton.GetComponent<Button>();
    29.         btn.onClick.AddListener(PickUp);
    30.     }
    31.  
    32.    
    33.     private void OnValidate()
    34.     {
    35.         if (inventory == null)
    36.             inventory = FindObjectOfType<Inventory>();
    37.  
    38.         if (spriteRenderer == null)
    39.               spriteRenderer = GetComponentInChildren<SpriteRenderer>();
    40.  
    41.     }
    42.  
    43.     private void OnTriggerEnter2D(Collider2D other)
    44.     {
    45.         if (other.gameObject.CompareTag("Player"))
    46.         {
    47.             pickUpButton.gameObject.SetActive(true);
    48.             isInRange = true;
    49.             spriteRenderer.enabled = true;
    50.         }
    51.     }
    52.  
    53.     private void OnTriggerExit2D(Collider2D other)
    54.     {
    55.         if (other.gameObject.CompareTag("Player"))
    56.         {
    57.             pickUpButton.gameObject.SetActive(false);
    58.             isInRange = false;
    59.             spriteRenderer.enabled = true;
    60.         }
    61.     }
    62.  
    63.  
    64.     public void PickUp()
    65.     {
    66.         // Code for UI pick up button
    67.         if (isInRange && !isEmpty)
    68.         {
    69.         inventory.AddItem(item.GetCopy());
    70.         amount--;
    71.         isEmpty = true;
    72.         spriteRenderer.color = emptyColor;
    73.         pickUpButton.gameObject.SetActive(false);
    74.         Destroy(this.gameObject);
    75.         }
    76.     }
    77. }
     
  8. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    Code (CSharp):
    1. public class Inventory : MonoBehaviour
    2. {
    3. [FormerlySerializedAs("Items")]
    4. [SerializeField] List<Item> startingItems;
    5. [SerializeField] Transform itemsParent;
    6. [SerializeField] ItemSlot[] itemSlots;
    7.  
    8. public event Action<ItemSlot> OnPointerEnterEvent;
    9. public event Action<ItemSlot> OnPointerExitEvent;
    10. public event Action<ItemSlot> OnRightClickEvent;
    11. public event Action<ItemSlot> OnLeftClickEvent;
    12. public event Action<ItemSlot> OnBeginDragEvent;
    13. public event Action<ItemSlot> OnEndDragEvent;
    14. public event Action<ItemSlot> OnDragEvent;
    15. public event Action<ItemSlot> OnDropEvent;
    16.  
    17. private void Start()
    18. {
    19. for (int i = 0; i < itemSlots.Length; i++)
    20. {
    21. itemSlots.OnLeftClickEvent += OnLeftClickEvent;
    22. itemSlots.OnPointerEnterEvent += OnPointerEnterEvent;
    23. itemSlots.OnPointerExitEvent += OnPointerExitEvent;
    24. itemSlots.OnRightClickEvent += OnRightClickEvent;
    25. itemSlots.OnBeginDragEvent += OnBeginDragEvent;
    26. itemSlots.OnEndDragEvent += OnEndDragEvent;
    27. itemSlots.OnDragEvent += OnDragEvent;
    28. itemSlots.OnDropEvent += OnDropEvent;
    29. }
    30.  
    31. SetStartingItems();
    32. }
    33.  
    34. private void OnValidate()
    35. {
    36. if (itemsParent != null)
    37. itemSlots = itemsParent.GetComponentsInChildren<ItemSlot>();
    38.  
    39. SetStartingItems();
    40. }
    41.  
    42.  
    43. private void SetStartingItems()
    44. {
    45. int i = 0;
    46. for (; i < startingItems.Count && i < itemSlots.Length; i++)
    47. {
    48. itemSlots.Item = startingItems.GetCopy();
    49. itemSlots.Amount = 1;
    50. }
    51.  
    52. for (; i < itemSlots.Length; i++)
    53. {
    54. itemSlots.Item = null;
    55. itemSlots.Amount = 0;
    56. }
    57. }
    58.  
    59. public bool AddItem(Item item)
    60. {
    61. for (int i = 0; i < itemSlots.Length; i++)
    62. {
    63. if (itemSlots.Item == null || (itemSlots.Item.ID == item.ID && itemSlots.Amount < item.MaximumStacks))
    64. {
    65. itemSlots.Item = item;
    66. itemSlots.Amount++;
    67. return true;
    68. }
    69. }
    70. return false;
    71. }
    72.  
    73. public bool RemoveItem(Item item)
    74. {
    75. for (int i = 0; i < itemSlots.Length; i++)
    76. {
    77. if (itemSlots.Item == item)
    78. {
    79. itemSlots.Amount--;
    80. if (itemSlots[i].Amount == 0) {
    81. itemSlots[i].Item = null;
    82. }
    83. return true;
    84.  
    85. }
    86. }
    87. return false;
    88. }
    89.  
    90. public Item RemoveItem(string itemID)
    91. {
    92. for (int i = 0; i < itemSlots.Length; i++)
    93. {
    94. Item item = itemSlots[i].Item;
    95. if (item != null && item.ID == itemID)
    96.  
    97. itemSlots[i].Amount--;
    98. if (itemSlots[i].Amount == 0) {
    99. itemSlots[i].Item = null;
    100. }
    101. return item;
    102. }
    103. return null;
    104. }
    105.  
    106. public bool IsFull()
    107. {
    108. for (int i = 0; i < itemSlots.Length; i++)
    109. {
    110. if (itemSlots[i].Item == null)
    111. {
    112. return false;
    113. }
    114. }
    115. return true;
    116. }
    117.  
    118. public int ItemCount(string itemID)
    119. {
    120. int number = 0;
    121.  
    122. for (int i = 0; i < itemSlots.Length; i++)
    123. {
    124. if (itemSlots[i].Item.ID == itemID)
    125. {
    126. number++;
    127. }
    128. }
    129. return number;
    130. }
     
  9. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    Code (CSharp):
    1. public class InventoryInput : MonoBehaviour
    2. {
    3.     [SerializeField] GameObject equipmentPanelGameObject;
    4.     [SerializeField] GameObject characterPanelGameObject;
    5.     [SerializeField] KeyCode[] toggleCharacterPanelKeys;
    6.     [SerializeField] KeyCode[] toggleInventoryKeys;
    7.  
    8.     void Update()
    9.     {
    10.         for (int i = 0; i < toggleCharacterPanelKeys.Length; i++)
    11.         {
    12.             if (Input.GetKeyDown(toggleCharacterPanelKeys[i]))
    13.             {
    14.                 characterPanelGameObject.SetActive(!characterPanelGameObject.activeSelf);
    15.                 break;
    16.             }
    17.         }
    18.  
    19.            
    20.         for (int i = 0; i < toggleInventoryKeys.Length; i++)
    21.         {
    22.             if (Input.GetKeyDown(toggleInventoryKeys[i]))
    23.             {
    24.                 if (!characterPanelGameObject.activeSelf)
    25.                 {
    26.                     characterPanelGameObject.SetActive(true);
    27.                     equipmentPanelGameObject.SetActive(false);
    28.                 }
    29.                 else if (equipmentPanelGameObject.activeSelf)
    30.                 {
    31.                     equipmentPanelGameObject.SetActive(false);
    32.                 }
    33.                 else
    34.                 {
    35.                     characterPanelGameObject.SetActive(false);
    36.                 }
    37.                 break;
    38.             }
    39.         }
    40.     }
    41.    
    42.  
    43.     public void ToggleEquipmentPanel()
    44.     {
    45.         equipmentPanelGameObject.SetActive(!equipmentPanelGameObject.activeSelf);
    46.     }
    47.     public void ToggleInventoryPanel()
    48.     {
    49.         characterPanelGameObject.SetActive(!characterPanelGameObject.activeSelf);
    50.     }
    51.  
    52. }
     
  10. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    Code (CSharp):
    1.  
    2. public class EquipmentPanel : MonoBehaviour
    3. {
    4.     [SerializeField] Transform equipmentSlotsParent;
    5.     [SerializeField] EquipmentSlot[] equipmentSlots;
    6.  
    7.     public event Action<ItemSlot> OnPointerEnterEvent;
    8.     public event Action<ItemSlot> OnPointerExitEvent;
    9.     public event Action<ItemSlot> OnRightClickEvent;
    10.     public event Action<ItemSlot> OnBeginDragEvent;
    11.     public event Action<ItemSlot> OnEndDragEvent;
    12.     public event Action<ItemSlot> OnDragEvent;
    13.     public event Action<ItemSlot> OnDropEvent;
    14.  
    15.     private void Start()
    16.     {
    17.         for (int i = 0; i < equipmentSlots.Length; i++)
    18.         {
    19.             equipmentSlots[i].OnPointerEnterEvent += OnPointerEnterEvent;
    20.             equipmentSlots[i].OnPointerExitEvent += OnPointerExitEvent;
    21.             equipmentSlots[i].OnRightClickEvent += OnRightClickEvent;
    22.             equipmentSlots[i].OnBeginDragEvent += OnBeginDragEvent;
    23.             equipmentSlots[i].OnEndDragEvent += OnEndDragEvent;
    24.             equipmentSlots[i].OnDragEvent += OnDragEvent;
    25.             equipmentSlots[i].OnDropEvent += OnDropEvent;
    26.         }
    27.     }
    28.  
    29.     private void OnValidate()
    30.     {
    31.         equipmentSlots = equipmentSlotsParent.GetComponentsInChildren<EquipmentSlot>();
    32.     }
    33.  
    34.     public bool AddItem(EquippableItem item, out EquippableItem previousItem)
    35.     {
    36.         for (int i = 0; i < equipmentSlots.Length; i++)
    37.         {
    38.             if (equipmentSlots[i].EquipmentType == item.EquipmentType)
    39.             {
    40.                 previousItem = (EquippableItem)equipmentSlots[i].Item;
    41.                 equipmentSlots[i].Item = item;
    42.                 return true;
    43.             }
    44.         }
    45.         previousItem = null;
    46.         return false;
    47.     }
    48.  
    49.     public bool RemoveItem(EquippableItem item)
    50.     {
    51.         for (int i = 0; i < equipmentSlots.Length; i++)
    52.         {
    53.             if (equipmentSlots[i].Item == item)
    54.             {
    55.                 equipmentSlots[i].Item = null;
    56.                 return true;
    57.             }
    58.         }
    59.         return false;
    60.     }
    61. }
     
  11. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    Code (CSharp):
    1. public class Character : MonoBehaviour
    2. {
    3.     public int Health = 50;
    4.  
    5.     public CharacterStat Strength;
    6.     public CharacterStat Agility;
    7.     public CharacterStat Intelligence;
    8.     public CharacterStat Vitality;
    9.  
    10.     [SerializeField] Inventory inventory;
    11.     [SerializeField] EquipmentPanel equipmentPanel;
    12.     [SerializeField] StatPanel statPanel;
    13.     [SerializeField] ItemTooltip itemTooltip;
    14.     [SerializeField] Image draggableItem;
    15.  
    16.     private ItemSlot dragItemSlot;
    17.  
    18.     private void OnValidate()
    19.     {
    20.         if (itemTooltip == null)
    21.             itemTooltip = FindObjectOfType<ItemTooltip>();
    22.     }
    23.  
    24.     private void Awake()
    25.     {
    26.         statPanel.SetStats(Strength, Agility, Intelligence, Vitality);
    27.         statPanel.UpdateStatValues();
    28.  
    29.         inventory.OnLeftClickEvent += Use;
    30.  
    31.         inventory.OnRightClickEvent += Equip;
    32.         equipmentPanel.OnRightClickEvent += Unequip;
    33.  
    34.         inventory.OnPointerEnterEvent += ShowTooltip;
    35.         equipmentPanel.OnPointerEnterEvent += ShowTooltip;
    36.  
    37.         inventory.OnPointerExitEvent += HideTooltip;
    38.         equipmentPanel.OnPointerExitEvent += HideTooltip;
    39.  
    40.         inventory.OnBeginDragEvent += BeginDrag;
    41.         equipmentPanel.OnBeginDragEvent += BeginDrag;
    42.  
    43.         inventory.OnEndDragEvent += EndDrag;
    44.         equipmentPanel.OnEndDragEvent += EndDrag;
    45.  
    46.         inventory.OnDragEvent += Drag;
    47.         equipmentPanel.OnDragEvent += Drag;
    48.  
    49.         inventory.OnDropEvent += Drop;
    50.         equipmentPanel.OnDropEvent += Drop;
    51.     }
    52.  
    53.     private void Use(ItemSlot itemSlot)
    54.     {
    55.         if (itemSlot.Item is UsableItem)
    56.         {
    57.             UsableItem usableItem = (UsableItem)itemSlot.Item;
    58.             usableItem.Use(this);
    59.            
    60.             if (usableItem.IsConsumable)
    61.             {
    62.                 inventory.RemoveItem(usableItem);
    63.                 usableItem.Destroy();
    64.             }
    65.         }
    66.     }
    67.  
    68.  
    69.     private void Equip(ItemSlot itemSlot)
    70.     {
    71.         if (itemSlot.Item is EquippableItem)
    72.         {
    73.             Equip((EquippableItem)itemSlot.Item);
    74.         }
    75.     }
    76.  
    77.     private void Unequip(ItemSlot itemSlot)
    78.     {
    79.         EquippableItem equippableItem = itemSlot.Item as EquippableItem;
    80.         if (equippableItem != null)
    81.         {
    82.             Unequip(equippableItem);
    83.         }
    84.     }
    85.  
    86.     private void ShowTooltip(ItemSlot itemSlot)
    87.     {
    88.         if (itemSlot.Item != null)
    89.         {
    90.             itemTooltip.ShowTooltip(itemSlot.Item);
    91.         }
    92.     }
    93.  
    94.     private void HideTooltip(ItemSlot itemSlot)
    95.     {
    96.         itemTooltip.HideTooltip();
    97.     }
    98.  
    99.     private void BeginDrag(ItemSlot itemSlot)
    100.     {
    101.         if (itemSlot.Item != null)
    102.         {
    103.             dragItemSlot = itemSlot;
    104.             draggableItem.sprite = itemSlot.Item.Icon;
    105.             draggableItem.transform.position = Input.mousePosition;
    106.             draggableItem.enabled = true;
    107.         }
    108.     }
    109.  
    110.     private void EndDrag(ItemSlot itemSlot)
    111.     {
    112.         dragItemSlot = null;
    113.         draggableItem.enabled = false;
    114.     }
    115.  
    116.     private void Drag(ItemSlot itemSlot)
    117.     {
    118.         if (draggableItem.enabled)
    119.         {
    120.             draggableItem.transform.position = Input.mousePosition;
    121.         }
    122.     }
    123.  
    124.     private void Drop(ItemSlot dropItemSlot)
    125.     {
    126.         if (dragItemSlot == null) return;
    127.  
    128.         if (dropItemSlot.CanReceiveItem(dragItemSlot.Item) && dragItemSlot.CanReceiveItem(dropItemSlot.Item))
    129.         {
    130.             EquippableItem dragItem = dragItemSlot.Item as EquippableItem;
    131.             EquippableItem dropItem = dropItemSlot.Item as EquippableItem;
    132.  
    133.             if (dragItemSlot is EquipmentSlot)
    134.             {
    135.                 if (dragItem != null) dragItem.Unequip(this);
    136.                 if (dropItem != null) dropItem.Equip(this);
    137.             }
    138.             if (dropItemSlot is EquipmentSlot)
    139.             {
    140.                 if (dragItem != null) dragItem.Equip(this);
    141.                 if (dropItem != null) dropItem.Unequip(this);
    142.             }
    143.  
    144.             statPanel.UpdateStatValues();
    145.  
    146.             Item draggedItem = dragItemSlot.Item;
    147.             int draggedItemAmount = dragItemSlot.Amount;
    148.  
    149.             dragItemSlot.Item = dropItemSlot.Item;
    150.             dragItemSlot.Amount = dropItemSlot.Amount;
    151.  
    152.             dropItemSlot.Item = draggedItem;
    153.             dropItemSlot.Amount = draggedItemAmount;
    154.         }
    155.     }
    156.  
    157.  
    158.  
    159.     public void Equip(EquippableItem item)
    160.     {
    161.         if (inventory.RemoveItem(item))
    162.         {
    163.             EquippableItem previousItem;
    164.             if (equipmentPanel.AddItem(item, out previousItem))
    165.             {
    166.                 if (previousItem != null)
    167.                 {
    168.                     inventory.AddItem(previousItem);
    169.                     previousItem.Unequip(this);
    170.                     statPanel.UpdateStatValues();
    171.                 }
    172.                 item.Equip(this);
    173.                 statPanel.UpdateStatValues();
    174.             }
    175.             else
    176.             {
    177.                 inventory.AddItem(item);
    178.             }
    179.         }
    180.     }
    181.  
    182.     public void Unequip(EquippableItem item)
    183.     {
    184.         if (!inventory.IsFull() && equipmentPanel.RemoveItem(item))
    185.         {
    186.             item.Unequip(this);
    187.             statPanel.UpdateStatValues();
    188.             inventory.AddItem(item);
    189.         }
    190.     }
    191. }
     
  12. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    These are the main scripts that the inventory uses, I would think if there was something that was causing the Inventory reference to work properly in the Unity editor but not mobile, it's be somewhere in these.
     
  13. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,559
    Spamming blocks of code into the forum won't be a substitute for actual debugging, which Praetor suggests you doing above.

    Since you might have missed Praetor's suggestion, I'll add my own to save you time and clutter in the forum:

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  15. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    Did you never try on mobile phone real device and unity editor are very different I test my games every time I add new big feature or for time period. I think you should disable them one by one but I think every script need other script for work
     
  16. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    Cool, thanks guys. I think I can get it working myself with all this top-notch advice.
     
    kader1081 likes this.
  17. JasonZeno

    JasonZeno

    Joined:
    Mar 22, 2023
    Posts:
    16
    This issue has been solved, all I had to do was change one function from OnValidate to Awake in the above ItemBox script. Maybe one of you guys would have noticed that if you didn't always immediately jump to copy and paste replies about rules and spamming. I swear I try my best to not use forums because the humanity has progressively left online forums. Thanks anyway.
     
    kader1081 likes this.
  18. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    First time ? I thought I was only one thinking like you.
     
    JasonZeno likes this.
  19. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,559
    I think you might have the order reversed here.

    You may be thinking that you page us to study your code and do your work.

    That's never going to scale because you're not going to advance in your skills whatsoever when people do your work for you and tell you what to change.

    Instead, since we see probably upwards of a dozen posts a day saying some variation of "doesn't work...", which your post does right here:

    So when you write:

    the answer is YES, you are missing something. You're not debugging.

    Now you have grown and become a better software engineer. I am thrilled because that is the intention of doing ANY of this anyway, and that is the goal of my posting helpful hints and guides.
     
  20. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,955
    Closing. Low-effort. Please start with the Learn Section.
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.