@xiangqihumaben

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

unityの敵オブジェクトを消すプログラムについての質問

解決したいこと

unityでランダムに敵を出現させ、returnキーで敵オブジェクトを一つ消すプログラムを組んでいるのですが、エラーは出ないものの敵オブジェクトを消す処理が実行されません。改善策を出していただきたいです。

以下プログラム
using System.Collections;
using UnityEngine;

public class syutugen : MonoBehaviour
{
[SerializeField] private GameObject[] enemyPrefabs;
[SerializeField] private Transform LeftTop;
[SerializeField] private Transform RightBottom;
[SerializeField] private int spawnCount = 100;
[SerializeField] private float spawnInterval = 3.0f;

private float minX, maxX, minY, maxY;

private void Start()
{
    minX = LeftTop.position.x;
    maxX = RightBottom.position.x;
    minY = RightBottom.position.y;
    maxY = LeftTop.position.y;

    StartCoroutine(SpawnEnemy());
}

private KeyCode destroyKey = KeyCode.Space; // 削除に使用するキー

void Update()
{
    // 指定したキーが押された場合
    if (Input.GetKey(destroyKey))
    {
        // 敵オブジェクトを削除
        Destroy(this.gameObject);
    }
}

private IEnumerator SpawnEnemy()
{
    for (int i = 0; i < spawnCount; i++)
    {
        Vector2 position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
        GameObject enemy = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
        Instantiate(enemy, position, Quaternion.identity, transform);
        yield return new WaitForSeconds(spawnInterval);
    }
}

}

0 likes

2Answer

private KeyCode destroyKey = KeyCode.Space; // 削除に使用するキー

削除に使用するキーがreturnキーではなくSpaceキーになってるからじゃない?

1Like

Your answer might help someone💌