2016-04-05 6 views
0

olarak çağırabiliyorsunuz ShowMoney sınıfında updateGold() öğesini çağırmam gerekiyor ancak başarısız oldu.neden A sınıfında bir işlevi çağıramıyor ancak B sınıfı

Başarı durumunda, konsolda para göstergesini göstermesi gerekir, ancak hiçbir şey göstermez.

Ve golText.text güncelleştirilmiyor.

UpdateGold() öğesinde de Debug.Log ("OK") olarak değiştirdiğimde, konsolda hiçbir şey göstermeyin.

Ancak Money sınıfında updateGold() öğesini çağırdığımda, bu başarı. Fark ne?

================================= Void ve hala sorun daha sonra bir sonraki yöntemle yalnızca erişilebilir Korumalı yöntem olun çözülmüş değil önce ===========

//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

'' burada diyemezsiniz ', herhangi bir hata aldığınız anlamına gelir? –

+0

@ Şanssız bir hata yok, sadece tepki yok. Çalışırsa, önce konsolda paragold göstermelidir, ancak hiçbir şey olmaz. Ve golText.text güncellenmiyor. –

+0

Ne 'ama başarısız' ne anlama geliyor? Nasıl başarısız oldu? –

cevap

0

yönteminizi tanımlayın.

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(); 
} 
+0

1.make Awake() public yerine bunun yerine benzer bir betik var mı? 2. Awake() öğesinden once updateGold() öğesini tanımlayın. Bunları deniyorum ama yine de aynı sonucu elde ediyorum –