【发布时间】:2018-03-26 14:14:36
【问题描述】:
我正试图在我走进我的“黄金”对象后立即重生并重置分数计数。 至于现在我什至无法重生,这在尝试实施“Score-Stuff”之前是可能的(起初“FoundGold”脚本仅用于能够重生)。我也试图让最低分数成为高分。 请注意,我是 C# 的新手,我将我需要的教程中的所有内容都放在一起,因此非常感谢您提供一些实际代码/说明出现问题的答案。
//GoldFound Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoldFound : MonoBehaviour
{
private ScoreManager theScoreManager;
public Transform target;
[SerializeField] private Transform player;
[SerializeField] private Transform respawnpoint;
private void Start()
{
theScoreManager = FindObjectOfType<ScoreManager>();
}
private void OnTriggerEnter(Collider other)
{
theScoreManager.scoreIncreasing = false;
player.transform.position = respawnpoint.transform.position;
theScoreManager.scoreCount = 0;
theScoreManager.scoreIncreasing = true;
}
}
其他代码
//ScoreManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public Text scoreText;
public Text hiScoreText;
public float scoreCount;
public float hiScoreCount;
public float pointPerSecond;
public bool scoreIncreasing;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (scoreIncreasing)
{
scoreCount += pointPerSecond * Time.deltaTime;
}
if(scoreCount > hiScoreCount)
{
hiScoreCount = scoreCount;
}
scoreText.text = "Score: " + Mathf.Round (scoreCount);
hiScoreText.text = "High Score: " + Mathf.Round (hiScoreCount);
}
}
【问题讨论】:
-
我觉得你应该做的第一件事就是分开你的任务。制定得分重置方法,一些用于重置球员位置等……然后制定适当使用它们的行为,尝试做所有硬编码的事情,这会导致一大碗意大利面条代码。通过拆分它们,您首先保证它们可以工作,然后您可以按照自己的意愿使用它们,而无需修改您的初始代码。
-
另外,将每次更新时的文本更改为相同的值确实是多余的,您可能正在修改 UI 元素,我问,
OnGUI()在哪里?其次,你应该只在分数发生变化时才刷新分数,而不是每一帧。 -
第三,我不明白你为什么在一次调用中将
theScoreManager.scoreIncreasing设置为true和false,因为它从未在两者之间使用过,它基本上总是true这让我无法理解它的目的。 -
如果你想让最低分成为你最好的,只需反转你的 if 语句的条件:
if(scoreCount < hiScoreCount) { hiScoreCount = scoreCount; } -
好的,如果你能提供一些反馈那就太好了,但我会试着了解你想要做什么,你似乎想做一种跑步某种游戏,你走得越快,你的分数越低,它就越好。也许它正在完成一个迷宫或什么的。从我所看到的情况来看,您如何使用
bool theScoreManager.scoreIncreasing进行操作是无用的。这绝对是真的,至少在接触过 GoldFound 的东西之后是这样。我也没有真正看到对它的系统的需求。没有你也可以这样做。