88799


[マルチポスト？(関連質問)](https://teratail.com/questions/n98mf58onbz5ib)

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

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

[![画像の説明をここに入力](https://i.stack.imgur.com/RkzkN.png)](https://i.stack.imgur.com/RkzkN.png)

[![画像の説明をここに入力](https://i.stack.imgur.com/dlikx.png)](https://i.stack.imgur.com/dlikx.png)

##### WeaponPool.cs

[code]

    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;
    
        }
    
    
    }
    
    
[/code]

##### EquipManager.cs

[code]

    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);
            }
        }
    }
    
    
[/code]


CC BY-SA 4.0
2022-03-29T00:41:18.133
88081
2022-05-13T07:48:04.437
2022-03-29T02:45:23.930
-1
question
c# unity3d
Unity void Start()の優先順位を変更する方法


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

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

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


CC BY-SA 4.0
2022-05-13T07:48:04.437
88799
2022-05-13T07:48:04.437
52599
88081
answer