无法将一些代码添加到我的积分计数器脚本中
Can't add some code into my point counter script
我有这个脚本,目前运行良好,我想添加一些额外的代码,这样如果我击中带有不同标签的对象,它会减去 300 分。换句话说,这是运行良好的脚本:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class POINTS : MonoBehaviour
{
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 300;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count >= 10000)
{
winText.text = "Congrats! To play again press <";
}
}
}
而且我认为这应该可行(请注意,我在 count = count + 300 之后添加了一些代码,因此理论上它应该开始减去点数
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class POINTS : MonoBehaviour
{
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 300;
if (other.gameObject.CompareTag("TestSolid"))
{
other.gameObject.SetActive(false);
count = count - 300;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count >= 10000)
{
winText.text = "Congrats! To play again press <";
}
}
}
但是如果我尝试 运行 它说 SetCountText 尚未声明。我如何让它工作?我对脚本编写很陌生,如果这是一个愚蠢的问题,我很抱歉。谢谢!
缺少右括号。我认为你应该学习如何阅读编译器输出。
好的。在评论中说一些有意义的东西是相当痛苦的,所以我会把它作为答案。
我不太喜欢 Unity,但看起来你存储 Count 属性 的地方可能不是最好的。看起来您正在扩展 class 命名行为,它可以是例如每次在 canvas 上发生某些事件时创建的临时对象。我的意思是你必须了解这个平台。阅读规范。
可以解决您的问题的是将 Count 存储在更合适的地方。 IE。在主要游戏对象中(如果我正确理解您所说的计数的含义)
代码如下所示:
if (other.gameObject.CompareTag("TestSolid"))
{
other.gameObject.SetActive(false);
Game.Count -= Settings.HitSolidDamage;
SetCountText();
}
我有这个脚本,目前运行良好,我想添加一些额外的代码,这样如果我击中带有不同标签的对象,它会减去 300 分。换句话说,这是运行良好的脚本:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class POINTS : MonoBehaviour
{
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 300;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count >= 10000)
{
winText.text = "Congrats! To play again press <";
}
}
}
而且我认为这应该可行(请注意,我在 count = count + 300 之后添加了一些代码,因此理论上它应该开始减去点数
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class POINTS : MonoBehaviour
{
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 300;
if (other.gameObject.CompareTag("TestSolid"))
{
other.gameObject.SetActive(false);
count = count - 300;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count >= 10000)
{
winText.text = "Congrats! To play again press <";
}
}
}
但是如果我尝试 运行 它说 SetCountText 尚未声明。我如何让它工作?我对脚本编写很陌生,如果这是一个愚蠢的问题,我很抱歉。谢谢!
缺少右括号。我认为你应该学习如何阅读编译器输出。
好的。在评论中说一些有意义的东西是相当痛苦的,所以我会把它作为答案。 我不太喜欢 Unity,但看起来你存储 Count 属性 的地方可能不是最好的。看起来您正在扩展 class 命名行为,它可以是例如每次在 canvas 上发生某些事件时创建的临时对象。我的意思是你必须了解这个平台。阅读规范。 可以解决您的问题的是将 Count 存储在更合适的地方。 IE。在主要游戏对象中(如果我正确理解您所说的计数的含义) 代码如下所示:
if (other.gameObject.CompareTag("TestSolid"))
{
other.gameObject.SetActive(false);
Game.Count -= Settings.HitSolidDamage;
SetCountText();
}