简体   繁体   中英

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

I'm doing some practice with Unity. What I'm basically trying to do is by using the singleton design pattern from the gameManager script. 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. 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.

I have a gameManager empty object assigned to the gameManager script

Here's the script for 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:

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() is called when another GameObject enters the collider of the GameObject the script is attached to. This is not the place to check for keyboard presses.

What you probably want to use is the Update() monobehavior. 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. gameObject refers to the gameObject the script it's mentioned in is attached to. This means when you do Destroy(gameObject) in the gameManager script you are destroying the object that the gameManager script it attached to.

To get rid of the GameObject called target you need to get access to it. You can do that in a couple ways:

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

This directly searches for a GameObject called "target" and destroys it. 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.

2) Create a public variable (like public GameObject target ) and store the physical instance of the target in it through the inspector. Then when you want to destroy it, just do Destroy(target) ;

This is how I would most likely do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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