Domanda

I would like to blur the background when I press pause. My blur effect is on the Blur Effect script which is situated in the Main Camera gameobject. The main problem is that when I press pause it gives me the error "Object reference not set to an instance of an object", but I have set the GameObject to its variable in Unity.

Here's what I have in the script about all this;

var camera:GameObject;

This is positioned in the Update()

if(paused == true){
    camera.GetComponent(BlurEffect).enabled = true;
}
if(paused == false){
    camera.GetComponent(BlurEffect).enabled = false;
}
È stato utile?

Soluzione

It seems that the camera variable isn't initialized ("not set to an instance of an object"). Use Camera.main to obtain a reference to the main camera. The following line in Update() should be enough:

Camera.main.GetComponent(BlurEffect).enabled = paused;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top