简体   繁体   English

如何调试为什么我的Unity3D游戏不增加得分?

[英]How to debug why my Unity3D game does not increase score?

I'm doing some practice with Unity. 我正在与Unity一起练习。 What I'm basically trying to do is by using the singleton design pattern from the gameManager script. 我基本上想做的是使用gameManager脚本中的单例设计模式。 I want to prompt the player to hit space, causing score to increase after each time, health to decrease after each time, and money increase once health reaches 0 and the cube is destroyed. 我想提示玩家击中空格,使得分每次增加,每次降低生命值,并且当生命值达到0且魔方被破坏时金钱增加。 I think I got some script assignments mixed up or I'm not sure how to reference the right game object. 我想我有些脚本分配错了,或者我不确定如何引用正确的游戏对象。 For whatever reason, hitting space won't increase score or decrease health. 无论出于何种原因,击中空格都不会增加得分或降低健康状况。

I got 3 texts: score, health, and money, along with a cube in the middle of the scene, with the script, DestroyBySpace, assigned to it. 我得到了3个文本:得分,健康状况和金钱,以及场景中间的一个立方体,并为其分配了脚本DestroyBySpace。

I have a gameManager empty object assigned to the gameManager script 我有一个分配给gameManager脚本的gameManager空对象

Here's the script for Game Manager: 这是Game Manager的脚本:

private int currentScore;
private int currentHealth;
private int currentMoney;

public Text scoreText;
public Text healthText;
public Text moneyText;

public static GameManager instance;

void Start()
{
    currentScore = 0;
    currentHealth = 20;
    currentMoney = 0;
    updateScore();
    updateMoney();
    updateHealth();
}

void Awake()
{
    instance = this;
}

public void adjustScore(int newScore)
{
    currentScore += newScore;
    updateScore();
}

public void adjustHealthAndMoney(int newHealth, int newMoney)
{
    currentHealth -= newHealth;
    updateHealth();

    if (currentHealth == 0)
    {          
        currentMoney += newMoney;
        updateMoney();
        Destroy(gameObject);
    }
}

void updateScore()
{
    scoreText.text = "Score: " + currentScore;
}

void updateHealth()
{
    healthText.text = "Health: " + currentHealth;
}

void updateMoney()
{
    moneyText.text = "Money: " + currentMoney;
}

and here's my script for DestroyBySpace: 这是我的DestroyBySpace脚本:

public int scoreValue;
public int healthValue;
public int moneyValue;
public GameObject target;
public GameManager gameManager;

void Start()
{
    GameObject gameManagerObject = GameObject.FindWithTag("GameManager");

    if (gameManagerObject != null)
    {
        gameManager = gameManagerObject.GetComponent<GameManager>();
    }

    if (gameManager == null)
    {
        Debug.Log("Cannot find Game Manager script");
    }
}

void onTriggerEnter()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        GameManager.instance.adjustHealthAndMoney(healthValue,moneyValue);
        GameManager.instance.adjustScore(scoreValue);
    }
}

If you could help steer me in the right direction, I would appreciate it. 如果您能引导我朝正确的方向发展,我将不胜感激。

I think you've mixed up what the monobehavior onTriggerEnter() does. 我认为您已经混淆了onTriggerEnter()行为。

onTriggerEnter() is called when another GameObject enters the collider of the GameObject the script is attached to. 当另一个GameObject进入脚本附加到的onTriggerEnter()的碰撞器时,将调用onTriggerEnter() This is not the place to check for keyboard presses. 这里不是检查键盘按键的地方。

What you probably want to use is the Update() monobehavior. 您可能要使用的是Update()行为。 It runs every frame, so you are guaranteed that if the user presses a key the function will be able to see it. 它每帧运行一次,因此可以确保如果用户按下某个键,该功能将能够看到它。

Your code would look something like this: 您的代码如下所示:

void Update(){
    if (Input.GetKeyDown(KeyCode.Space))
    {
        GameManager.instance.adjustHealthAndMoney(healthValue,moneyValue);
        GameManager.instance.adjustScore(scoreValue);
    }
}

Also, I think you misunderstood what gameObject refers to when you did Destroy(gameObject) in the gameManager script. 另外,我认为您在gameManager脚本中执行Destroy(gameObject)时误解了gameObject指的是什么。 gameObject refers to the gameObject the script it's mentioned in is attached to. gameObject指的是附加了脚本的gameObject。 This means when you do Destroy(gameObject) in the gameManager script you are destroying the object that the gameManager script it attached to. 这意味着当您在gameManager脚本中执行Destroy(gameObject) ,您将销毁其附加到gameManager脚本的对象。

To get rid of the GameObject called target you need to get access to it. 要摆脱称为目标的GameObject,您需要访问它。 You can do that in a couple ways: 您可以通过以下几种方法来做到这一点:

1) Destroy(GameObject.Find("target")); 1)Destroy(GameObject.Find(“ target”));

This directly searches for a GameObject called "target" and destroys it. 这将直接搜索称为“目标”的GameObject并将其销毁。 The downside to this is that it requires the GameObject to be called exactly "target" and if there are multiple GameObjects called "target" it will only destroy the first one. 不利的一面是,它要求将GameObject精确地称为“目标”,如果有多个称为“ target”的GameObject,则只会销毁第一个。

2) Create a public variable (like public GameObject target ) and store the physical instance of the target in it through the inspector. 2)创建一个公共变量(如public GameObject target ),并通过检查器将目标的物理实例存储在其中。 Then when you want to destroy it, just do Destroy(target) ; 然后,当您想销毁它时,只需执行Destroy(target)

This is how I would most likely do it. 这就是我最有可能这样做的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM