【发布时间】: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;
}
}
【问题讨论】: