2017-08-26 39 views
2

我嘗試在unity3d c#中實現一個評分系統,但它似乎沒有任何反應我已經添加標籤到對象是它附加腳本,並沒有錯誤,但我嘗試添加分數或其他沒有改變。 我有對象和對撞機元素活躍 但是當玩家碰撞其破壞對象,但它不添加比分錢和水嘗試添加比分錢或水的錯誤

這是觸發代碼

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

public class waterpicks : MonoBehaviour { 

void OnTriggerEnter ( Collider other ){ 
    if (other.tag == "water_bottle1") { 
     scoreManager.money += 10; 
     scoreManager.score += 10; 
     scoreManager.water += 1; 

     Destroy(other.gameObject); 
    } 

} 
} 

,這是我的評分系統

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

public class scoreManager : MonoBehaviour { 

public static int score; 
public static int money; 
public static int level; 
public static int water; 
public static int drinks; 
public Text ShowScore; 
public Text Showmoney; 
public Text Showlevel; 
public Text Showwater; 

//public static int frutoscoleta; 
// Use this for initialization 
void Start() { 
    score = 0; 
    money = 0; 
    level = 0; 
    water = 80; 
    //PlayerPrefs.GetInt ("scorePref"); 
    //score = PlayerPrefs.GetInt ("scorePref"); 
    //PlayerPrefs.GetInt ("moneyPref"); 
    //money = PlayerPrefs.GetInt ("moneyPref"); 
    //PlayerPrefs.GetInt ("levelPref"); 
    //level = PlayerPrefs.GetInt ("levelPref"); 
    ShowScore.text = "Score : " + score; 
    Showmoney.text = "Money : " + money; 
    Showlevel.text = "Level : " + level; 
    Showwater.text = "Drinks : " + drinks; 
} 



// Update is called once per frame 
void Update() { 
    //PlayerPrefs.SetInt ("scorePref", score); 
    //PlayerPrefs.SetInt ("moneyPref", money); 
    //PlayerPrefs.SetInt ("level", level); 
    //scoreManager.score++; 
    //scoreManager.money++; 
    //scoreManager.level++; 

} 


} 
+0

你是什麼意思,什麼都沒有發生? – z3nth10n

+0

@Ikillnukes我的意思是玩家碰撞瓶子的水應該破壞物品瓶子,然後添加10分10分和1分的飲料。但是,我的玩家與她所銷燬的瓶子發生碰撞,但是沒有在這種情況下添加其他腳本中的變量的指針scoreManager.cs – ines

+0

你怎麼知道錢沒有被添加?調試?? – z3nth10n

回答

2

你需要「秀」開始外文本,在更新是一個選項...

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

public class scoreManager : MonoBehaviour { 

    public static int score; 
    public static int money; 
    public static int level; 
    public static int water; 
    public static int drinks; 
    public Text ShowScore; 
    public Text Showmoney; 
    public Text Showlevel; 
    public Text Showwater; 

    //public static int frutoscoleta; 
    // Use this for initialization 
    void Start() { 
     score = 0; 
     money = 0; 
     level = 0; 
     water = 80;  
    }  

    // Update is called once per frame 
    void Update() {    
     ShowScore.text = "Score : " + score; 
     Showmoney.text = "Money : " + money; 
     Showlevel.text = "Level : " + level; 
     Showwater.text = "Drinks : " + drinks; 
    }  
} 
+2

這應該工作,但它是更好的OP更新文本只有當變量是更新而不是每幀都做。 – Programmer

4

該代碼在Start函數中運行。 Start函數只運行一次。函數Update每幀都會運行,並且您的增量代碼應該在啓動函數之外運行。

因爲您正在執行少量字符串連接,所以更新Update函數中的Text組件並不是一個好主意。用一個函數替換所有增量代碼(如scoreManager.money += 10scoreManager.score += 10;),然後更新各個函數中的Text組件,以便Text組件爲,只有在相應變量更新時更新。

public class Waterpicks : MonoBehaviour 
{ 
    ScoreManager scoreManager; 

    void Start() 
    { 
     GameObject obj = GameObject.Find("ScoreMangerHolder"); 
     scoreManager = obj.GetComponent<ScoreManager>(); 
    } 

    void OnTriggerEnter(Collider other) 
    { 
     if (other.tag == "water_bottle1") 
     { 
      scoreManager.incrementMoney(10); 
      scoreManager.incrementScore(10); 
      scoreManager.incrementWater(1); 

      Destroy(other.gameObject); 
     } 
    } 
} 

評分系統

public class ScoreManager : MonoBehaviour 
{ 

    public static int score; 
    public static int money; 
    public static int level; 
    public static int water; 
    public static int drinks; 
    public Text ShowScore; 
    public Text Showmoney; 
    public Text Showlevel; 
    public Text Showwater; 

    //public static int frutoscoleta; 
    // Use this for initialization 
    void Start() 
    { 
     score = 0; 
     money = 0; 
     level = 0; 
     water = 80; 

     ShowScore.text = "Score : " + score; 
     Showmoney.text = "Money : " + money; 
     Showlevel.text = "Level : " + level; 
     Showwater.text = "Drinks : " + drinks; 
    } 

    public void incrementMoney(int amount) 
    { 
     money += amount; 
     Showmoney.text = "Money : " + money; 
    } 

    public void incrementScore(int amount) 
    { 
     score += amount; 
     ShowScore.text = "Score : " + score; 
    } 

    public void incrementWater(int amount) 
    { 
     water += amount; 
     Showwater.text = "Water : " + water; 
    } 
} 

這說明你的代碼應該是什麼樣子。注意類名的大小寫。我認爲你應該這樣做。您可以繼續添加其他功能來更新我未包含的文本。您還可以使incrementXXX函數爲static並直接調用它,但這不是必需的。

相關問題