In Watch windows; in monoDev i have: Name Value (F) ic1.itemSprite (null) (C) ic1.itemSprite == null false (C) ic1.itemSprite != null false
Unity does some weird overrides, so direct null checking can sometimes yield strange results. But the code that produces this would certainly help.
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): void OnTriggerEnter2D(Collider2D collision) { if(collision.gameObject.tag == "Pickup") { ItemClass ic = collision.transform.gameObject.GetComponent<ItemClass>(); GameObject go = GameObject.Find("Inv1"); // Take the first slot inventary ItemClass ic1 = go.GetComponent<ItemClass>(); // and take his ItemClass to take if Inv1 have any sprite (then is not available) Debug.Log(ic1.itemSprite); // Show - null - in console //null //UnityEngine.Debug:Log(Object) //Arghh. What my mind say is not the same what i see! Icannot enter to AddToSlot(go,ic); Arghh if (ic1.itemSprite == null) // IF slot is available, then overwrite with pickable object properties. AddtoSlot(go,ic); } public void AddtoSlot(GameObject go, ItemClass ic) { go.GetComponent<ItemClass>().itemSprite = ic.itemSprite; ((UnityEngine.UI.Image)go.GetComponentInChildren<UnityEngine.UI.Image>()).sprite = ic.itemSprite; } Problem is that Debug.Log(ic1.itemSprite); show null but conditional if (ic1.itemSprite == null) doesn't enter in AddtoSlot(go,ic);;
Have you tried if(!itemSprite) ? Like I said, the bool and equality overrides can be a little squirrelly at times.
" if (!itemSprite) " What!! BoreMormon It work!!! What happens!! I need to learn C# again! So null = false ! Arggh im a bit confused!!
ItemClass ic1 = go.GetComponent<ItemClass>(); Code (csharp): using UnityEngine; using System.Collections; [System.Serializable] public class ItemClass : MonoBehaviour { public AudioClip audioclip; public string itemName; public Sprite itemSprite; void OnTriggerEnter2D(Collider2D coll) { if(audioclip !=null) AudioSource.PlayClipAtPoint(audioclip, transform.position); Destroy(gameObject); } } 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/)
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.