2017-12-11 3 views
0

내 돈이 내 방에서 몇 방패를 사려고 현장에 있어야한다. 왠지 내 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를 넣어 곳입니다 내 shield.power 스크립트

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

입니다. (강조 파란색).

+0

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

답변

2

텍스트는 UI에서 문자열을 렌더링하기위한 UI 구성 요소입니다. 각 장면마다 고유 한 텍스트 구성 요소가 있어야합니다. 그런 다음 돈을 다루는 모든 스크립트에 자체 MoneyAmount 변수가 없도록 한 곳에 돈을 보관해야합니다. 돈 스크립트는 다음과 같이 수 : 당신이 돈을 사용할 때마다

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"라는 정적 속성이 있습니다. 이 속성은 정적이므로 앱의 어느 위치에서나 값을 읽을 수 있습니다. –