通过脚本测试预制实例化/克隆

Testing prefab instantiation / cloning through scripting

我有一个脚本,其中有一个 fade 预制件!

以下代码片段在 运行 时间内实例化预制件,如附图所示。

public void Begin( float fadingTime ) { this.fadingTime = 褪色时间;

fade = Instantiate(fadePrefab) as GameObject;
fade.transform.parent = Camera.main.transform;
fade.transform.localPosition = new Vector3( 0, 0, 0.31f );
fade.transform.localRotation = Quaternion.identity;

不过我仍然收到此错误消息:

NullReferenceException: 对象引用未设置到对象的实例 SceneManager.Begin(单衰落时间)(在 Assets/Scripts/SceneManager.cs:57)

鉴于我可以看到预制件是 克隆的,我很困惑为什么会收到此错误消息。请看附图

所以,我想也许我应该在代码中包含一些东西来查看预制件是否确实被实例化了,但我不知道如何测试它。

比如,有时候我们会放这样一个东西,看看有没有什么价值:

Debug.Log( "I get to this point" + someVariableValue );

我要测试什么或如何进行此测试,因为我不知道为什么我收到这个错误消息...

谢谢,

可能 Camera.main null 如果您的场景中没有启用相机。

将名为 MainCamera 的相机添加到您的场景以解决问题。