Search Unity

NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by unity_42Cdh19RwEXXJA, Mar 21, 2019.

  1. unity_42Cdh19RwEXXJA

    unity_42Cdh19RwEXXJA

    Joined:
    Nov 29, 2018
    Posts:
    3
    The error message says the problem is at line 21. I have a number of panels with an image that I drag and drop into another panel. Every time I try to drag from one panel to the other in play mode, I get the error message.

    Code (CSharp):
    1. using System.Reflection;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. public class DropMe : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
    7. {
    8.     public Image containerImage;
    9.     public Image receivingImage;
    10.     private Color normalColor;
    11.     public Color highlightColor = Color.yellow;
    12.    
    13.     public void OnEnable ()
    14.     {
    15.         if (containerImage != null)
    16.             normalColor = containerImage.color;
    17.     }
    18.    
    19.     public void OnDrop(PointerEventData data)
    20.     {
    21.             containerImage.color = normalColor;
    22.        
    23.        
    24.         if (receivingImage == null)
    25.             return;
    26.        
    27.         Sprite dropSprite = GetDropSprite (data);
    28.         if (dropSprite != null)
    29.             receivingImage.overrideSprite = dropSprite;
    30.     }
    31.  
    32.     public void OnPointerEnter(PointerEventData data)
    33.     {
    34.         if (containerImage == null)
    35.             return;
    36.        
    37.         Sprite dropSprite = GetDropSprite (data);
    38.         if (dropSprite != null)
    39.             containerImage.color = highlightColor;
    40.     }
    41.  
    42.     public void OnPointerExit(PointerEventData data)
    43.     {
    44.         if (containerImage == null)
    45.             return;
    46.        
    47.         containerImage.color = normalColor;
    48.     }
    49.    
    50.     private Sprite GetDropSprite(PointerEventData data)
    51.     {
    52.         var originalObj = data.pointerDrag;
    53.         if (originalObj == null)
    54.             return null;
    55.        
    56.         var dragMe = originalObj.GetComponent<DragMe>();
    57.         if (dragMe == null)
    58.             return null;
    59.        
    60.         var srcImage = originalObj.GetComponent<Image>();
    61.         if (srcImage == null)
    62.             return null;
    63.        
    64.         return srcImage.sprite;
    65.     }
    66. }
    67.  
     
  2. jasrei

    jasrei

    Joined:
    Apr 19, 2018
    Posts:
    15
    Seems like you need a null check at line 20.
     
  3. unity_42Cdh19RwEXXJA

    unity_42Cdh19RwEXXJA

    Joined:
    Nov 29, 2018
    Posts:
    3
    That's what I thought originally too, I already tried

    Code (CSharp):
    1.         if(normalColor != null)
    2.             containerImage.color = normalColor;
    but I got the same error message.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    normalColor isn't the thing you need to null check. containerImage is the one that would be causing the issue.
     
    Joe-Censored likes this.
  5. unity_42Cdh19RwEXXJA

    unity_42Cdh19RwEXXJA

    Joined:
    Nov 29, 2018
    Posts:
    3
    Thank you. My compiler issues are fixed. Now I can fix the other problems.