如何在 Unity (C#) 中实例化游戏对象以及实例化的整数?

How can I instantiate game objects in Unity (C#) along with an instantiated integer?

我正在尝试为游戏中实例化的敌人提供生命值。 一开始敌人只是一发子弹就被消灭了。

我认为添加一个新的 class 来保持实例化预制件的 HP 会起作用,但我不知道如何正确编写。要么它是 "static" 然后我知道如何从子弹控制器调用它 class,但它没有实例化并且为所有实例化的敌人保留相同的 int 值,或者它不是 "static"然后我不知道如何从其他 classes.

调用它

这是子弹控制器当前的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletController : MonoBehaviour
{
    private Transform bullet;
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        bullet = GetComponent<Transform>();
    }
    void FixedUpdate()
    {
        bullet.position += transform.up * speed;
        if (bullet.position.y >= 10)
            Destroy(gameObject);
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Enemy")
        {
            Enemy4HP.health--;
            Destroy(gameObject);
            if (Enemy4HP.health < 1)
            {
                Destroy(other.gameObject);
                PlayerScore.playerScore++;
            }
        }
        if (other.tag == "Enemy2")
        {
            Enemy10HP.health--;
            Destroy(gameObject);
            if (Enemy10HP.health <1)
            {
                Destroy(other.gameObject);
                PlayerScore.playerScore++;
            }
        }

    }
}

为了两个健康 classes:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy4HP : MonoBehaviour
{
    public int health = 4;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy10HP : MonoBehaviour
{
    public int health = 10;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

这就是敌人的实例化方式:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EnemyController : MonoBehaviour
{
    private Transform enemyHolder;
    public float speed;
    public GameObject shot;
    public GameObject enemy;
    public GameObject enemy2;
    public Text winText;
    int secCount;
    float timer = 0;
    public float fireRate = 0.997f;
    public int enemyCount;
    // Start is called before the first frame update
    void Start()
    {
        enemyCount = 0;
        secCount = 0;
        enemyHolder = GetComponent<Transform>();
        winText.enabled = false;
        InvokeRepeating("MoveEnemy", 0f, 0.016f);
    }
    private List<GameObject> allSpawns = new List<GameObject>();

    void MoveEnemy()
    {
        float xPosition = Random.Range(-11f, 11f);
        int enemyType = Random.Range(0, 8);
        secCount = Random.Range(2, 4);
        timer += Time.deltaTime;
        if (timer >= secCount && enemyCount < 25)
        {
            if (enemyType > 0)
            {
                GameObject spawned = Instantiate(enemy, new Vector3(xPosition, 6, 0), Quaternion.identity);
                allSpawns.Add(spawned);
            }
            else
            {
                GameObject spawned = Instantiate(enemy2, new Vector3(xPosition, 6, 0), Quaternion.identity);
                allSpawns.Add(spawned);
            }
            enemyCount++;
            timer = timer - secCount;
        }
        foreach (GameObject thisEnemy in allSpawns)
        {
            if (thisEnemy !=null)
            {
                thisEnemy.transform.position += new Vector3(0, -1 * speed * Time.deltaTime, 0);
            }
        }
        if (PlayerScore.playerScore == 25)
        {
            timer = 0;
            CancelInvoke();
            InvokeRepeating("MoveEnemy2", 0f, 0.016f);
        }
    }
 ...

即returns"An object reference is required for the non_static field..."。我能做些什么? 谢谢

最简单最快。而不是 2 类 和 2 个不同的数字使 1 称为 EnemyHP。将组件添加到敌人预制件中,并在检查器中的预制件上将组件的生命值设置为敌人 1 的 4 和敌人 2 的 10。然后:

void OnTriggerEnter2D(Collider2D other)
{
    //you can probably just make both enemy the same tag.
    if (other.tag == "Enemy" || other.tag == "Enemy2")
    {
        //get the Hp component of the specific enemy.
        EnemyHP hpComponent = other.gameObject.GetComponent<EnemyHP>();
        hpComponent.health--;
        Destroy(gameObject);
        if (hpComponent.health < 1)
        {
            Destroy(other.gameObject);
            PlayerScore.playerScore++;
        }
    }

}