(Unity6)ボスのバリアを破壊する

(サンプルコード)

★「DestroyBoss」スクリプトの一部変更

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

public class DestroyBoss : MonoBehaviour
{
    public GameObject effectPrefab;
    public GameObject effectPrefab2;
    public int objectHP;
    public GameObject itemPrefab;
    public int scoreValue;
    public int damage = 1;
    private ScoreManager sm;

    // ★変更(publicに変更)
    public bool barrier = true;

    private void Start()
    {
        sm = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (barrier == true)
        {
            return;
        }

        if (other.CompareTag("Shell"))
        {
            objectHP -= damage;

            if (objectHP > 0)
            {
                Destroy(other.gameObject);
                GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
                Destroy(effect, 2.0f);
            }
            else
            {
                sm.AddScore(scoreValue);
                Destroy(other.gameObject);
                GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
                Destroy(this.gameObject);

                if (itemPrefab)
                {
                    Vector3 pos = transform.position;
                    Instantiate(itemPrefab, new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
                }
            }
        }
    }
}

★新規にC#スクリプトの作成

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

public class GameController : MonoBehaviour
{
    private GameObject[] barriers;
    public GameObject bossBariier;
    public DestroyBoss destroyBoss;
    public AudioClip sound;

    void Update()
    {
        barriers = GameObject.FindGameObjectsWithTag("Barrier.");

        if (barriers.Length == 0)
        {
            // ボスのバリアを破壊する
            Destroy(bossBariier.gameObject);

            // ボスにダメージが入るようにする
            destroyBoss.barrier = false;

            // バリアが壊れる音を出す
            AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
        }
    }
}

(設定)

・新規にCreateEmptyオブジェクトを作成

・名前を「GameController」に変更