为什么不能在A类中调用函数,但可以在B类中调用它

时间:2016-04-05 03:55:47

标签: c# unity3d

我需要在ShowMoney类中调用updateGold()但是失败了。

如果成功,它应该在控制台中显示money.gold,但不显示任何内容。

并且golText.text不会更新。

即使我在updateGold()中更改为Debug.Log(" OK"),也不会在控制台中显示任何内容。

但是当我在Money类中调用updateGold()时,它就成功了。有什么区别?

=============================================== ==========

//Prop.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Prop : MonoBehaviour,IPointerClickHandler
{
    public int goldPrice;
    public int diamondPrice;
    public int propID;

    public GameObject goldError;
    public GameObject diamondError;
    public GameObject purchasePanel;

    //check if purchased when back to this Level
    void Awake()
    {
        if(Bag.propIsPurchased[propID]==true)
        {
            alreadySold();
        }
    }

    //show purchase panel and add delegate
    public void OnPointerClick (PointerEventData eventData)
    {
        purchasePanel.SetActive(true);
        ConfirmPurchase.ensureOperation += this.purchase;
        CancelPurchase.cancelOperation += this.cancel;
    }

    public void purchase()
    {
        if (goldPrice>0)
        {
            if(goldPrice > Money.gold)
            {
                goldError.SetActive(true);
            }
            else
            {
                Money.payGold(goldPrice);
                ShowMoney.updateGold();//problem here
                Bag.addProp(propID);
                alreadySold();
            }
        }
        //...       
    }
//...

//ShowMoney.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ShowMoney : MonoBehaviour 
{
    public GameObject goldText;
    public GameObject diamondText;

    public static Text golText;
    public static Text diaText;

    void Awake()
   {
        golText = goldText.GetComponent<Text>();
        diaText = diamondText.GetComponent<Text>();
        updateGold();
        updateDiamond();
    }

    public static void updateGold()
    {
        Debug.Log(Money.gold);
        golText.text = Money.gold.ToString();
    } 

    public static void updateDiamond()
    {       
        diaText.text = Money.diamond.ToString();
    }
}

//Money.cs
using UnityEngine;
using System.Collections;

public class Money: MonoBehaviour
{
    public static int gold = 100;
    public static int diamond = 20;

    public static Money instance;
    void Awake()
    {
        instance = this;
    }   

    public static void earnGold(int gol)
    {
        gold += gol;
    }

    public static void earnDiamond(int dia)
    {
        diamond += dia;
    }

    public static void payGold(int gol)
    {
        gold -= gol;
        //ShowMoney.updateGold();//can work here
    }

    public static void payDiamond(int dia)
    {
        diamond -= dia; 
    }
}

1 个答案:

答案 0 :(得分:0)

定义你的方法在Void之前仍然没有解决问题然后制作只能通过下一个方法访问的受保护方法。

public static void updateGold()
{
    Debug.Log(Money.gold);
    golText.text = Money.gold.ToString();
} 

public static void updateDiamond()
{       
    diaText.text = Money.diamond.ToString();
}

public void Awake()
{
    golText = goldText.GetComponent<Text>();
    diaText = diamondText.GetComponent<Text>();
    updateGold();
    updateDiamond();
}