有人可以告诉我为什么我的游戏对象得分值在游戏过程中在我的屏幕上没有增加但是在我的检查员中。我正在创建一个隐藏的对象游戏,每次点击一个对象时,分数都会逐渐上升。我展示的第一个脚本是附加到所有对象并且正在点击的点击... clickobj
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;
}
}
答案 0 :(得分: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);
}