-1

boxタグでゲームオブジェクトを削除すると、複数回書いても複数削除されません。

ボックス プレハブを Instantiate でコピーすると、プレハブでボックス コライダーが有効になっているにもかかわらず、ボックス コライダーが無効になることもわかりました。

私は C# にかなり慣れていないので、自分のコードがうまく最適化されているとは思えません。

using System.Collections.Generic;
using UnityEngine;

public class RestartLevel : MonoBehaviour
{
    float currentLevel = 1f;
    public GameObject boxPrefab;
    public GameObject firstBox;
    Vector3 a = new Vector3(5, 5, 5);

    void Update()
    {
        if (Input.GetButtonDown("Reset"))
        {
            if (currentLevel == 1f)
            {
                Destroy(GameObject.FindWithTag("box"));
                Destroy(GameObject.FindWithTag("box"));
                Destroy(GameObject.FindWithTag("box"));

                GameObject newBox = Instantiate(boxPrefab);
                newBox.transform.position = a;
            }
        }
    }
}
4

1 に答える 1