2017-04-23 189 views
1

我知道關於統一得分等方面有很多問題,但是我發現很難真正爲我的遊戲構建代碼,並且已經查看了大量的YouTube教程和博客帖子。我想知道是否有人可以幫助我添加分數,以便每次在隱藏對象遊戲中單擊我的一個對象時,它都會提供分數。我真的很感激。到目前爲止,我已經添加了一個名爲Score.cs的單獨腳本。代碼如下。然後我將它添加到我的背景圖像中,並將腳本包含在每個對象中,我的評分文本位於畫布內,並且當前不計算任何被點擊的對象。Unity遊戲得分

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

public class Score : MonoBehaviour { 

    public Text scoreText; 
    public int gameObject; 

    private int score; 
    // Use this for initialization 

    void Start() { 
     score = 0; 
     UpdateScore(); 
    } 
    void OnMouseDown() { 
     score += gameObject; 
     UpdateScore(); 
    } 
    // Update is called once per frame 
    void UpdateScore() { 
     scoreText.text = "Score:\n" + score; 

    } 
} 

!*****編輯(Score.cs)*****!

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

public class Score : MonoBehaviour { 

    public Text scoreText; 
    public int gameObject; 

    public int score; 
    // Use this for initialization 

    void Start() { 
     score = 0; 
     UpdateScore(); 
    } 
    void OnMouseDown() { 
     score += gameObject; 
     UpdateScore(); 
    } 
    // Update is called once per frame 
    void UpdateScore() { 
     scoreText.text = "Score:\n" + score; 

    } 
} 

!****** Clicker.cs ******!

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine.EventSystems; 
using UnityEngine; 

public class Clicker : MonoBehaviour, IPointerClickHandler 
{ 

    Score score; 

    void Start() 
    { 
     addPhysics2DRaycaster(); 
     //Get Score Script Instance 
     string scoreObject = "gameObject"; 
     score = GameObject.Find(scoreObject).GetComponent<Score>(); 
    } 
    void addPhysics2DRaycaster() 
    { 
     Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>(); 
     if (physicsRaycaster == null) 
     { 
      Camera.main.gameObject.AddComponent<Physics2DRaycaster>(); 
     } 
    } 

    public void OnPointerClick(PointerEventData eventData) 
    { 
     //Click detected. Increment score 
     score.score++; 
     Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name); 

    } 

} 
+0

*我點擊我的隱藏對象遊戲中的一個對象,它提供了一個分數。「*你是什麼意思* *提供了一個分數」*? – Programmer

+0

@programmer,對不起,我應該描述它好一點,我的意思是,當玩家點擊每個對象時,得分會逐漸上升 – Rochelle

回答

0

這真的很容易。創建一個檢測你的GameObjects點擊的腳本。你可以使用OnPointerClick。從該腳本獲取Score腳本的實例,然後在調用OnPointerClick時遞增得分變量。

確保將private int score;更改爲public int score;

將腳本附加到要檢測點擊的每個對象上。

public class Clicker : MonoBehaviour, IPointerClickHandler 
{ 
    Score score; 

    void Start() 
    { 
     //Get Score Script Instance 
     string scoreObject = "GameObjectScoreIsAttachedTo"; 
     score = GameObject.Find(scoreObject).GetComponent<Score>(); 
    } 

    public void OnPointerClick(PointerEventData eventData) 
    { 
     //Click detected. Increment score 
     score.score++; 

    } 
} 

如果你的對象是2D,你將需要添加addPhysics2DRaycaster()功能。如果是3D您需要添加addPhysicsRaycaster()功能。有關更多信息,請參閱here

+0

非常感謝您回覆我,我只是嘗試過,並且仍然無法設法在用戶點擊分數後逐漸更新分數。接收到錯誤「NullReferenceException:對象引用未設置爲對象的實例 Clicker.Start()(在Assets/Clicker.cs:16)」 – Rochelle

+0

*「GameObjectScoreIsAttachedTo」*應該是GameObject的名稱,'Score '從你的問題附加到.....用那個GameObject的名字代替 – Programmer

+0

我剛剛試過,sti即使我使用的是GameObject的名稱,也會收到相同的消息。你介意我是否發送過我的遊戲給你看看,我將不勝感激 – Rochelle