当我尝试激活相机效果时显示 NullReferenceException
NullReferenceException displays when I'm trying to activate camera effect
当我尝试激活主相机上的效果时显示 NullReferenceException。
我的主相机上有一个小脚本:
using UnityStandardAssets.ImageEffects;
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
private SunShafts mySunShafts;
// Use this for initialization
void Start () {
mySunShafts = GetComponent<SunShafts>();
}
// Update is called once per frame
void Update () {
foreach (Camera c in GameObject.FindObjectsOfType(typeof(Camera))) {
if ((c.name == "Main Camera")) {
if ((c.transform.position.x > 6000)) {
mySunShafts.enabled = true;
}
}
}
}
}
构建过程成功,但是当我开始场景时,控制台一直说:
...我的效果根本没有激活。
为什么我会收到此异常以及如何解决此问题?
========================== 编辑 #1 ================ ==============
我认为脚本已经添加到“检查器”选项卡中。下面的屏幕截图显示了主相机的检查器选项卡。
(红色条表示我上面提到的脚本,红色箭头表示我想要激活的效果)
您似乎只对主摄像头上的这种行为感兴趣,因此只需将脚本附加到主摄像头即可。
修改如下,Using a foreach loop 和 Using GameObject.Find
in an Update
is lowefficiency and completely necessary here.
using UnityStandardAssets.ImageEffects;
using UnityEngine;
using System.Collections;
public class CameraSunShafts : MonoBehaviour {
private SunShafts mySunShafts;
// Use Awake for setting up references to components.
void Awake () {
mySunShafts = GetComponent<SunShafts>();
}
void Update () {
if (transform.position.x > 6000)
mySunShafts.enabled = true;
}
}
如果您将相机在其 X 位置上移动超过 6000 个单位,它将启用太阳轴
当我尝试激活主相机上的效果时显示 NullReferenceException。
我的主相机上有一个小脚本:
using UnityStandardAssets.ImageEffects;
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
private SunShafts mySunShafts;
// Use this for initialization
void Start () {
mySunShafts = GetComponent<SunShafts>();
}
// Update is called once per frame
void Update () {
foreach (Camera c in GameObject.FindObjectsOfType(typeof(Camera))) {
if ((c.name == "Main Camera")) {
if ((c.transform.position.x > 6000)) {
mySunShafts.enabled = true;
}
}
}
}
}
构建过程成功,但是当我开始场景时,控制台一直说:
...我的效果根本没有激活。
为什么我会收到此异常以及如何解决此问题?
========================== 编辑 #1 ================ ==============
我认为脚本已经添加到“检查器”选项卡中。下面的屏幕截图显示了主相机的检查器选项卡。 (红色条表示我上面提到的脚本,红色箭头表示我想要激活的效果)
您似乎只对主摄像头上的这种行为感兴趣,因此只需将脚本附加到主摄像头即可。
修改如下,Using a foreach loop 和 Using GameObject.Find
in an Update
is lowefficiency and completely necessary here.
using UnityStandardAssets.ImageEffects;
using UnityEngine;
using System.Collections;
public class CameraSunShafts : MonoBehaviour {
private SunShafts mySunShafts;
// Use Awake for setting up references to components.
void Awake () {
mySunShafts = GetComponent<SunShafts>();
}
void Update () {
if (transform.position.x > 6000)
mySunShafts.enabled = true;
}
}
如果您将相机在其 X 位置上移动超过 6000 个单位,它将启用太阳轴