Unity游戏得分

问题描述:

我知道关于统一得分等方面有很多问题,但是我发现很难真正为我的游戏构建代码,并且已经查看了大量的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

这真的很容易。创建一个检测你的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