NullReferenceException displays when I'm trying to activate an effect on my main camera.
I have a tiny script on my main camera:
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;
}
}
}
}
}
The Build process is sucessfull, but when I'm starting the scene, Console keeps saying the following:
...and my effect doesn't activate at all.
Why am I getting this exception and how could I resolve this issue?
========================== Edit #1 ==============================
I think that the script has already added to the Inspector tab. The screenshot below shows the Main Camera's Inspector tab. (Red bar indicates my above mentioned script and the red arrow indicates the effect I'd like to activate)
It seems like you're only interested in this behaviour on your main camera, so just attach the script to your main camera only.
Amends as follows, Using a foreach loop and Using GameObject.Find
in an Update
is inefficient and completely unnecessary 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;
}
}
If you move your camera over 6000 units on it's X position it will enable the Sun Shafts