【问题标题】:Score within game比赛内得分
【发布时间】:2017-04-29 11:14:10
【问题描述】:

有人可以告诉我为什么我的游戏对象得分值在玩游戏期间没有在我的屏幕上增加,而是在我的检查器中。我正在创建一个隐藏对象游戏,每次单击对象时,分数都会逐渐上升。我展示的第一个脚本是附加到所有对象并正在点击的那个... 点击对象

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

public class clickobj : MonoBehaviour, IPointerClickHandler
{

    Score score;

    void Start()
    {
        addPhysics2DRaycaster();
        //Get Score Script Instance
        string scoreObject = "office";
        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++;
        Destroy (gameObject);
        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);

    }

}

下一个脚本是我附加到场景中的 Score.cs 脚本,我还在脚本中放置了 score 对象,如下面的屏幕截图所示。 分数.cs

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

public class Score : MonoBehaviour {


    public int gameObject;

    public int score;
    public Text scoreText;
    // 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;

    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    将您的 OnPointerClick 更改为

    public void OnPointerClick(PointerEventData eventData)
    {
        //Click detected. Increment score
        score.score++;
        score.scoreText.text = "Score:\n" + score.score;
        Destroy (gameObject);
        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
    
    }
    

    【讨论】:

    • 嘿!我刚刚做了,但不幸的是它保持不变
    • 最后一件事!抱歉,您知道我如何将 0/14 添加到分数文本中,因此数字 0 开始像 1/14、2/14 等一样计数
    • 改成这个:score.scoreText.text = "Score:\n" + score.score + "/14";
    • 没问题,如果我的回答解决了您的问题,请将其标记为解决方案。谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 2015-10-07
    • 1970-01-01
    相关资源
    最近更新 更多