[英]GameObject creation order issue
I'm currently working on an inventory system in Unity3D, and came upon a weird problem. 我目前正在开发Unity3D中的库存系统,并遇到了一个奇怪的问题。 I have created non-MonoBehaviour classes for my inventory system (in case that matters), so that I have an Inventory class which holds a list of Slot objects, which in turn holds a list of Item objects.
我已经为我的库存系统创建了非MonoBehaviour类(如果重要的话),所以我有一个Inventory类,它包含Slot对象的列表,而Slot对象又包含Item对象的列表。
Then I added a component script to my "HudInventoryPanel", called "HudInventoryController", which looks like this: 然后我在我的“HudInventoryPanel”中添加了一个组件脚本,名为“HudInventoryController”,如下所示:
using UnityEngine;
using System.Collections;
public class HudSlotController : MonoBehaviour {
private InventoryController ic;
// Use this for initialization
void Start () {
ic = GetComponent<InventoryController>();
}
// Update is called once per frame
void Update () {
}
}
However, inside the Start() method, the InventoryController (part of my Player) hasn't been created yet, and it seems to me like the gameobjects are created in alphabetical order...? 但是,在Start()方法中,还没有创建InventoryController(我的播放器的一部分),在我看来,游戏对象是按字母顺序创建的......?
How should I deal with this problem? 我该如何处理这个问题?
You can specify script execution order in the project settings (Edit -> Project settings -> Script execution order), so use that to make sure your scripts are executed in the correct order. 您可以在项目设置中指定脚本执行顺序(编辑 - >项目设置 - >脚本执行顺序),因此请使用它来确保脚本按正确的顺序执行。 In addition to that, you can use the
Awake()
method as all Awake
s are executed before any Start()
method. 除此之外,您可以使用
Awake()
方法,因为所有Awake
都在任何Start()
方法之前执行。 Hope a combination of these helps you! 希望这些组合可以帮到你!
I solved this by creating an intermediate variable in my InventoryController script, plus creating a "manual" getter; 我通过在InventoryController脚本中创建一个中间变量,再创建一个“手动”getter来解决这个问题。 https://gist.github.com/toreau/f7110f0eb266c3c12f1b
https://gist.github.com/toreau/f7110f0eb266c3c12f1b
Not sure why I have to do it this way, though. 不知道为什么我必须这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.