My Object为null,但我仍然可以在其上调用函数(== null返回true)(getType返回myType)

时间:2019-09-10 07:11:46

标签: c# unity3d

我有一个对象列表,如果要删除该对象,我想删除引用该列表中特定对象的按钮。我的问题是,当我向该列表中添加一个对象时,一旦添加了该对象,则按钮会立即删除自身,因为它应该为空。

每个按钮都有一个具有Objects'name'属性(ProductName)的组件,并且我在列表中查找具有相同名称的Object,如果找到一个(确实如此),则不应返回null(但是确实)。

我在这里想念什么?

这里是有问题的Update方法(附加到按钮上)

    private void Update()
    {
        Product p = GameManager.Instance.shoppingCart.Find(n => n.pname.Equals(gameObject.GetComponentInParent<ProductName>().name));
        Debug.Log(p);
        Debug.Log("p is of type " + p.GetType());
        Debug.Log("p is null is " + (p == null));

        if (p == null)
        {
            Object.Destroy(gameObject);
        }
        if (ButtonIsClicked)
        {
            GameManager.Instance.AddProduct(p);
            ButtonIsClicked = false;
        }
    }

这是产品类别

public class Product : MonoBehaviour
{
    public string pname;
    public int quantity;
    [HideInInspector]
    public float price; // the price of the product times the quantity
    public float basePrice; // The price of a single unit

    override
    public string ToString()
    {
        CalcPrice();
        return pname + ": " + price + "€" + "   x" + quantity + "\n";
    }

    public void CalcPrice()
    {
        price = basePrice * quantity;
    }
}

这是生成按钮的更新功能

    void Update()
    {
        for (int i = amount; i < GameManager.Instance.shoppingCart.Count; amount++, i++)
        {
            GameObject go = Instantiate(ButtonPrefab, transform.position + new Vector3(200, Ypos, 3), Quaternion.identity);
            go.transform.parent = GameObject.Find("PopupPanel").transform;
            ProductName pname = go.AddComponent<ProductName>();
            pname.name = GameManager.Instance.shoppingCart.ElementAt(amount).pname;
            Ypos -= 30;
        }
    }

调试日志返回(按顺序):

调试产品:1欧元x1 //根据产品toString()函数

p属于产品类型

p为空为True

0 个答案:

没有答案