[英]Instantiated class with “new” keyword is null after Start method has ended and button is pressed. Unity3d
I have a SaveController, it looks like this: 我有一个SaveController,它看起来像这样:
In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController: 在MainMenuLogic类的Start方法中,我实例化SaveController的实例:
When the game starts, MusicOn method has been called before the Start method is called so the soundController is null... 游戏开始时,在调用Start方法之前已调用MusicOn方法,因此soundController为null ...
public void MusicOn(bool on)
{
saveController.MusicOn(on);
}
I've obviously not understood the application flow, so wondered if anyone has an idea on what might be going wrong. 我显然不了解应用程序流程,因此想知道是否有人对可能出问题的地方有所了解。 Which method should i instantiate the SaveController?
我应该实例化SaveController的哪种方法?
PS I know, GameObject.Find, bleurgh... i'm refactoring, no wuckers. 附言:我知道,GameObject.Find,勃艮第……我正在重构,没有老手。
Looks like Awake was the method i was looking for. 看起来Awake是我一直在寻找的方法。
Awake
is called before Start
, so doing the saveController
instantiation in Awake
allows for the instantiation to happen before MusicOn
is called. 在
Start
之前调用Awake
,因此在Awake
执行saveController
实例化允许实例化在调用MusicOn
之前MusicOn
。
void Awake()
{
saveController = new SaveController();
}
source: https://unity3d.com/learn/tutorials/topics/scripting/awake-and-start 来源: https : //unity3d.com/learn/tutorials/topics/scripting/awake-and-start
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.