2017-12-11 4 views
0

私は私のmoney.textを私の店のシーンにいくつかの盾を買うようにしたい。 何らかの理由で私のmoney.textは他のシーンにしかなく、店舗のシーンには入っていません。私の店のシーンにmoney.textを置く

これは私のショップスクリプトです。

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using UnityEngine.SceneManagement; 

public class ShopController : MonoBehaviour { 

int MoneyAmount; 

int isPowerup1Sold,isPowerup2Sold,isPowerup3Sold; 

public Text MoneyAmountText; 

public Button Buynow1,Buynow2,Buynow3; 

// Use this for initialization 
void Start() { 
    MoneyAmount = PlayerPrefs.GetInt ("moneyAmount"); 
} 

// Update is called once per frame 
void Update() { 

    MoneyAmountText.text = "Money : " + MoneyAmount.ToString(); 

    isPowerup1Sold = PlayerPrefs.GetInt ("isPowerup1Sold"); 
    isPowerup2Sold = PlayerPrefs.GetInt ("isPowerup2Sold"); 
    isPowerup3Sold = PlayerPrefs.GetInt ("isPowerup3Sold"); 

    if (MoneyAmount >= 50 && isPowerup1Sold == 0) 
     Buynow1.interactable = true; 
    else 
     Buynow1.interactable = false; 

    if (MoneyAmount >= 70 && isPowerup2Sold == 0) 
     Buynow2.interactable = true; 
    else 
     Buynow2.interactable = false; 

    if (MoneyAmount >= 120 && isPowerup3Sold == 0) 
     Buynow3.interactable = true; 
    else 
     Buynow3.interactable = false; 
} 

public void buyPowerup1() 
{ 
    MoneyAmount -= 50; 
    PlayerPrefs.SetInt ("isPowerup1Sold", 1); 
} 

public void buyPowerup2() 
{ 
    MoneyAmount -= 70; 
    PlayerPrefs.SetInt ("isPowerup2Sold", 1); 
} 

public void buyPowerup3() 
{ 
    MoneyAmount -= 120; 
    PlayerPrefs.SetInt ("isPowerup3Sold", 1); 
} 
public void exitshop() 
{ 
    PlayerPrefs.SetInt ("moneyAmount", MoneyAmount); 
    Application.LoadLevel ("levelselect"); 
} 

} 

これは私のお金のスクリプトです。

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
public class Money : MonoBehaviour { 

public Text MoneyText; 
public static int MoneyAmount = 0; 
// Use this for initialization 
void Start() { 
    MoneyAmount = PlayerPrefs.GetInt ("moneyAmount",0); 

} 

// Update is called once per frame 
void Update() { 
    MoneyText.text = "Money" + MoneyAmount.ToString(); 
} 
} 

これはこれは私がキャンバスのシーンで表示するように私のmoney.textを置く場所です。(ハイライトブルー色)

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
public class Money : MonoBehaviour { 

public Text MoneyText; 
public static int MoneyAmount = 0; 
// Use this for initialization 
void Start() { 
    MoneyAmount = PlayerPrefs.GetInt ("moneyAmount",0); 

} 

// Update is called once per frame 
void Update() { 
    MoneyText.text = "Money" + MoneyAmount.ToString(); 
} 
} 

私shield.powerスクリプトです。

+0

[1]:https://i.stack.imgur.com/EiKsG .png –

答えて

2

テキストは、UIで文字列を表示するためのUIコンポーネントです。各シーンには独自のテキストコンポーネントが必要です。 次に、金額を扱うすべてのスクリプトが独自のMoneyAmount変数を持たないように、金額を1か所に保持する必要があります。 マネースクリプトは次のようになります。あなたがお金を使うたび

public class Money : MonoBehaviour { 

    static int? cachedAmount = null; 
    const string playerPrefsKeyName = "moneyAmount"; 
    const int startMoney = 0; 

    public static int Amount { 
     get { 
      if (cachedAmount == null) { 
       cachedAmount = PlayerPrefs.GetInt (playerPrefsKeyName, startMoney); 
      } 
      return cachedAmount.Value; 
     } 
     set { 
      if (cachedAmount == null || value != cachedAmount.Value) { 
       PlayerPrefs.SetInt (playerPrefsKeyName, value); 
       cachedAmount = value; 
      } 
     } 
    } 
} 

が...そして、あなたが使用することができ

if (Money.Amount >= 70) { 
    Money.Amount -= 70; 
    MoneyAmountText.text = "Money : " + Money.Amount.ToString(); 
} 
+0

または私のお気に入りの解決策:シーンの使用をやめてください。 ;)確かに、Flashに至るまでの日付ですが、Unityでもうまくいきます。それは、シーン間のデータをシャトルするためにPlayerPrefsを使用していると言いましたか?*松葉杖のイエス・キリスト... – Draco18s

+0

金の間に本当にドットがあります。 –

+0

@ P.Degsはい。クラス "Money"には "Amount"という名前の静的プロパティがあります。このプロパティは静的なので、アプリ内のどこからでも値を読み取ることができます。 –

関連する問題