2016-12-01 127 views
0

我正在爲我的項目設計一款遊戲,並且在某個操作後得分(文本)不會更新。它堅持在0.在Unity C中不更新文本#

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class uiManager : MonoBehaviour { 

public Text scoreText; 
bool gameOver; 
int score; 

// Use this for initialization 
void Start() { 
    gameOver = false; 
    score = 0; 
    InvokeRepeating ("scoreUpdate", 1.0f, 0.5f); 
} 

// Update is called once per frame 
void Update() 
{ 
    scoreText.text = "Point: " + score; 
} 

void scoreUpdate() 
{ 
    if (gameOver == false) 
    { 
     score += 1; 
    } 
} 

public void gameOVER() 
{ 
    gameOver = true; 
} 

public void Play() 
{ 
    Application.LoadLevel ("MuachiJump"); 
} 

void OnCollisionEnter2D(Collision2D col) 
{ 
    if (col.gameObject.tag == "branch") 
    { 
     score += 1; 
     Destroy(col.gameObject); 
    } 
} 

我只是想確定在這段代碼中是否有任何錯誤?他們都似乎是正確的。

+0

請注意,您不應該按照原樣在更新中打印分數。它創建一個新的字符串,並丟棄每一幀的前一個字符串。相反,請在您的樂譜成員上使用一個也更新文本的屬性。這樣,它只會在您更改值時創建一個新的字符串。 – Everts

回答

3

代碼本身似乎很好。確保uiManager已附加到場景中處於活動狀態的對象。在Update方法中,如果添加例如Debug.Log(score),則應該每幀打印到日誌。如果沒有發生這種情況,您需要將腳本附加到場景中的對象,並確保Text對象具有有效的引用。

0

你有一個活動對象的腳本附加在你的場景?