Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Variable in inspector never changing + Object reference error

Discussion in 'Scripting' started by Emolk, Nov 28, 2019.

  1. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    I'm getting some weird interaction with my scripts. I have an itemDragHandler and an item drop handler seen below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class ItemDragHandler : MonoBehaviour, IDragHandler, IEndDragHandler
    7. {
    8.     [SerializeField] RectTransform m;
    9.     public InventoryItem item;
    10.     public ItemDropHandler itemDropHandler;
    11.  
    12.     void Start(){
    13.         itemDropHandler = ItemDropHandler.instance;
    14.     }
    15.     public void OnDrag(PointerEventData eventData){
    16.         transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, -5);
    17.  
    18.  
    19.  
    20.      
    21.         item = transform.parent.parent.GetComponent<InventorySlot>().item;
    22.      
    23.         itemDropHandler.setItem(item);
    24.         itemDropHandler.DraggedFrom = transform.parent.parent.gameObject;
    25.     }
    26.  
    27.     public void OnEndDrag(PointerEventData eventData){
    28.         m.anchoredPosition = Vector2.zero;
    29.     }
    30. }
    31.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine;
    5.  
    6. public class ItemDropHandler : MonoBehaviour, IDropHandler
    7. {
    8.     public static ItemDropHandler instance;
    9.     List<RectTransform> inventorySlotTransforms;
    10.     [SerializeField] GameObject[] inventorySlots;
    11.     public InventoryItem itemDragging;
    12.     public GameObject DraggedFrom;
    13.     public string bullshit;
    14.     [SerializeField] GameObject[] currentEquipmentSlots;
    15.     Inventory inventory;
    16.  
    17.     void Awake(){
    18.         instance = this;
    19.     }
    20.     void Start(){
    21.         inventorySlots = GameObject.FindGameObjectsWithTag("InventorySlot");
    22.         inventory = GameObject.Find("Player").GetComponent<Inventory>();
    23.         currentEquipmentSlots = GameObject.FindGameObjectsWithTag("CurrentEquipmentSlot");
    24.     }
    25.  
    26.     public void setItem(InventoryItem item){
    27.         print("Setting item + " + item.name);
    28.         itemDragging = item;
    29.         bullshit = item.name;
    30.     }
    31.     public void OnDrop(PointerEventData eventData){
    32.  
    33.         bool found = false;
    34.  
    35.         RectTransform invPanel = transform as RectTransform;
    36.  
    37.         if(!RectTransformUtility.RectangleContainsScreenPoint(invPanel, Input.mousePosition)){
    38.  
    39.         }
    40.         else{
    41.         }
    42.  
    43.         print(itemDragging.name);
    44.  
    45.         foreach (var slot in inventorySlots)
    46.         {
    47.             if(!found){
    48.                 var slotTransform = slot.transform as RectTransform;
    49.                 if(RectTransformUtility.RectangleContainsScreenPoint(slotTransform, Input.mousePosition)){
    50.                     if(DraggedFrom.tag == "InventorySlot"){
    51.                         print("Adding item");
    52.                         int oldIndex = slot.GetComponent<InventorySlot>().slotIndex;
    53.                         int newIndex = DraggedFrom.GetComponent<InventorySlot>().slotIndex;
    54.  
    55.                         slot.GetComponent<InventorySlot>().AddItem(itemDragging);
    56.                         DraggedFrom.GetComponent<InventorySlot>().ClearSlot();
    57.  
    58.                         inventory.swapSlots(oldIndex, newIndex);
    59.                         inventory.setPositions();
    60.                         found = true;
    61.                     }
    62.                     else{
    63.                         print("Swapping");
    64.                         int Index = slot.GetComponent<InventorySlot>().slotIndex;
    65.                         inventory.replace(Index, itemDragging);
    66.                     }
    67.                 }
    68.             }
    69.         }
    70.     }
    71. }
    72.  
    When i call the setItem function in ItemDropHandler from ItemDragHandler, the print statement is called correctly and prints the correct item name. However itemDragging and 'bullshit' never change in the inspector. No matter what i do i can never see these values change in the inspector. They definetely change however since they are used in the OnDrop method in ItemDropHandler which works with other elements with the same parent. However when i drop an item on any other canvas element i get an object reference error on line 43 of itemdrophandler. This shouldn't be happening since it shouldn't matter where i drop the item, itemDragging should always be set. Is there some weird functions of Unity's IDropHandler and IDragHandler, IEndDragHandler that prevents me from setting variables in classes which inherit from these?
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Is there more than one ItemDropHandler?
     
    Emolk likes this.
  3. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Yep.....

    I forgot to remove the old script component when i moved its location in the hierarchy...

    Punishment for bad practice i guess.

    Thanks :)