0

マルチポスト?(関連質問)

提示コードですがコンソール画面にある通りEquipManager.csvoid start()部のcross_Up.GetComponent<WeaponPool>().GetInstance();WeaponPool.csvois Start()関数のDebug.Log(ああああ)よりも先にGetInstance()関数のDebug.Log(eeee);が実行されてしまうためpool変数がnullで参照されてしまいます。ヒエラルキーの順序を変更しましたが同じでしたどうやって優先順位を変更してvoid Start()関数を先に実行するのでしょうか?

参考サイト:https://ekulabo.com/execution-order

画像の説明をここに入力

画像の説明をここに入力

WeaponPool.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponPool : MonoBehaviour
{
    Transform[] pool;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("ああああああ" + transform.childCount);
        pool = new Transform[transform.childCount];

        for (int i = 0; i < pool.Length; i++)
        {
            pool[i] = transform.GetChild(i);
        }

    }

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

    }





    /*############################ オブジェクトを返す ############################*/
    public Transform GetInstance()
    {
        if(pool == null)
        {
            Debug.Log("eeee");
        }


        foreach (Transform t in pool)
        {
            if (t.gameObject.activeSelf == false)
            {
                t.gameObject.SetActive(true);

                return t;
            }
        }

        GameObject obj = Instantiate(pool[0].gameObject,transform);
        obj.transform.position = transform.position;
        return obj.transform;

    }


}

EquipManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EquipManager : MonoBehaviour
{   

    [SerializeField] GameObject status;
    public enum CrossInputType
    {
        Up,
        Down,
        Left,
        Right
    }
    

    Animator animator;
    bool isShot = false;
    CrossInputType crossInput = CrossInputType.Up;
    WeaponParameter.WeaponType weaponType;

    WeaponParameter weaponParameter = new WeaponParameter();

    Player_Status nowStatus;
    [SerializeField] GameObject cross_Up;
    [SerializeField] GameObject cross_Left;
    [SerializeField] GameObject cross_Right;
    [SerializeField] GameObject cross_Down;
    Transform now;
    float reloadTime = 0;



    void Start()
    {
        animator = GetComponent<Animator>();

        crossInput = CrossInputType.Up;
        now = cross_Up.GetComponent<WeaponPool>().GetInstance();

        nowStatus = status.GetComponent<Player_Status>(); //レベルステータスを取得
        
        //Debug.Log("ああ " + nowStatus.getParameter.hp);
    }




    

    void Update()
    {
        


        if (Input.GetAxis("Cross_Horizontal") == 1)
        {

        }
        else if (Input.GetAxis("Cross_Horizontal") == -1)
        {

        }
        else if (Input.GetAxis("Cross_Vertical") == -1)
        {
            now.gameObject.SetActive(false);
            now = cross_Up.GetComponent<WeaponPool>().GetInstance();
        }
        else if (Input.GetAxis("Cross_Vertical") == 1)
        {

        }

            
        // 攻撃
        if ((Input.GetButton("R1") == true) && (isShot == false))
        {
            Shot();
            
            now = null;
            isShot = true;
        }

        if(isShot == true)
        {
            reloadTime += Time.deltaTime;
            if (reloadTime > status.GetComponent<Player_Status>().getParameter.reloadTime)
            {
                reloadTime = 0;
                   
                
                isShot = false;
            }
        }

        if (now != null)
        {
            now.transform.position = this.transform.position;
            //now.transform.rotation = Quaternion.LookRotation(Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)));
        }
    }

    private void Shot()
    {
        weaponParameter.damage = status.GetComponent<Player_Status>().getParameter.attack;

        if (WeaponParameter.WeaponType.Spear == weaponType)
        {
            now.GetComponent<Spear>().Shot(Camera.main.transform.forward,weaponParameter);
        }
    }
}

1 件の回答 1

0

スクリプトの実行順を制御する方法です。

https://tech.pjin.jp/blog/developer/2021/08/31/unity_script-execution-order

ただ、いろいろ注意点もあるみたいなので使う際は気を付けてください

この質問に回答するには、ログインする必要があります。