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

null != (null)

Discussion in 'Scripting' started by sirio21, May 22, 2015.

  1. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    In Watch windows; in monoDev i have:

    Name Value
    (F) ic1.itemSprite (null)
    (C) ic1.itemSprite == null false
    (C) ic1.itemSprite != null false

    o_O
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Can you show actual code, I'm not sure what you're trying to express there.
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Unity does some weird overrides, so direct null checking can sometimes yield strange results. But the code that produces this would certainly help.
     
  4. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    I have a KEY, POSION, ETC pickable objects; so each one have an ItemClass that define his properties (name, sprite to show, etc).

    So i need to pickup and show in an inventary, so i want to take the sprite and show in "Inv1" gameobject (my first slot of my inventary.


    Code (csharp):
    1.  
    2. void OnTriggerEnter2D(Collider2D collision) {
    3.  
    4.         if(collision.gameObject.tag == "Pickup") {
    5.             ItemClass ic = collision.transform.gameObject.GetComponent<ItemClass>();
    6.  
    7.             GameObject go = GameObject.Find("Inv1");  // Take the first slot inventary
    8.  
    9.             ItemClass ic1 = go.GetComponent<ItemClass>(); // and take his ItemClass to take if Inv1 have any sprite (then is not available)
    10.  
    11.             Debug.Log(ic1.itemSprite);  // Show  -   null   -   in console
    12. //null
    13. //UnityEngine.Debug:Log(Object)
    14.  
    15.            //Arghh. What my mind say is not the same what i see!  Icannot enter to AddToSlot(go,ic); Arghh
    16.             if (ic1.itemSprite == null)   // IF slot is available, then overwrite with pickable object properties.
    17.                 AddtoSlot(go,ic);
    18.         }
    19.  
    20. public void AddtoSlot(GameObject go, ItemClass ic) {
    21.         go.GetComponent<ItemClass>().itemSprite = ic.itemSprite;
    22.         ((UnityEngine.UI.Image)go.GetComponentInChildren<UnityEngine.UI.Image>()).sprite = ic.itemSprite;
    23.     }
    24.  
    Problem is that Debug.Log(ic1.itemSprite); show null but conditional if (ic1.itemSprite == null) doesn't enter in AddtoSlot(go,ic);;
     
    Last edited: May 22, 2015
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Have you tried if(!itemSprite) ?

    Like I said, the bool and equality overrides can be a little squirrelly at times.
     
  6. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    (I edit the code post,,)

    I will try now with if (!itemSprite)...
     
  7. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    " if (!itemSprite) "

    What!! BoreMormon It work!!!

    What happens!! I need to learn C# again! So null = false ! Arggh im a bit confused!!
     
  8. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    What type is: ic1.itemSprite ?? Can you show its declaration?
     
  9. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    ItemClass ic1 = go.GetComponent<ItemClass>();


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. [System.Serializable]
    5.  
    6. public class ItemClass : MonoBehaviour
    7. {
    8.     public AudioClip audioclip;
    9.     public string itemName;
    10.     public Sprite itemSprite;
    11.  
    12.     void OnTriggerEnter2D(Collider2D coll) {
    13.         if(audioclip !=null) AudioSource.PlayClipAtPoint(audioclip, transform.position);
    14.         Destroy(gameObject);
    15.     }
    16. }
    17.  
    :) Here work fine if(audioclip !=null) but with my custom class (ItemClass) doesn't work. mmm. so with objects in UnityEngine it work?

    Problem is that Unity cannot know the type of my class? (http://forum.unity3d.com/threads/null-check-inconsistency-c.220649/)
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yeah, this is not a c# thing. Its a Unity thing. Unity did some overriding of the bool operator and the equality operator. Using the bool operator is not the same thing as a null check, it checks if the object has been destroyed. But for all practical purposes a destroyed component should be treated as null.