在Start()中初始化后,Unity3D对象为空

时间:2017-09-02 14:44:31

标签: c# unity3d

我试图让一些对象和类进行交互。 我有两节课。代码类似。但是在class Building_main之后,在Start()中初始化了对象后,我可以在函数中重用它,在class House_main中我不能(对象为空)。class Building_main是{{1}的子节点}。

我做错了什么?

头等舱:

class House_main

第二课:

    public class Building_main : MonoBehaviour {

        public string owner;
        public string producedGoodName;

        private Building_goods goods;
        private House_main house;

        void Start () {
            goods = this.transform.Find("Building_goods").GetComponent<Building_goods>();
            house = this.transform.parent.gameObject.GetComponent<House_main> ();
            UpdateHouseInfo ();
        }

        void UpdateHouseInfo () {
            // reusing objs (house, goods) no error
            house.UpdateGoodInfo (goods.GetGoodByName("sword"), "sword");
        }

1 个答案:

答案 0 :(得分:0)

看起来我的子类( <div id="app"> <div class="wrapper"> <p>THIS IS MY CONTENT</p> </div> <footer> This is the footer </footer> </div>)Start()函数正在父类(// surely you don't want a copy? for(auto&& Variant : variantContainer) { auto should_break = false; // If the variant types match, set the value std::visit([this, &should_break](auto&& arg) { // arg is one of the Ts... in the variant this->doTheRightThing(arg); if (shouldCauseBreak(arg)) should_break = true; }, Variant); if (should_break) break; } )Start()函数之前执行。

因此我在调用class Building_main时尚未分配class House_main对象class House_main,因为我使用goods调用它来启动它()

function UpdateGoodInfo

不知道这个。