我目前正在开发Unity3D中的库存系统,并遇到了一个奇怪的问题 . 我已经为我的库存系统创建了非MonoBehaviour类(如果重要的话),所以我有一个Inventory类,它包含Slot对象的列表,而Slot对象又包含Item对象的列表 .
然后我在我的“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 () {
}
}
但是,在Start()方法内部,还没有创建InventoryController(我的播放器的一部分),在我看来,游戏对象是按字母顺序创建的......?
我该如何处理这个问题?
2 回答
您可以在项目设置中指定脚本执行顺序(编辑 - >项目设置 - >脚本执行顺序),因此请使用它来确保脚本按正确的顺序执行 . 除此之外,您可以使用
Awake()
方法,因为所有Awake
都在Start()
方法之前执行 . 希望这些组合可以帮到你!我通过在InventoryController脚本中创建一个中间变量,再创建一个"manual" getter来解决这个问题 . https://gist.github.com/toreau/f7110f0eb266c3c12f1b
不知道为什么我必须这样做 .