- Home /
How to change post-processing behaviour of a camera through script?
I have two post-processing behaviours with different settings and I would like to swap them when the player dies. Is that possible, and if so, how?
Answer by tanoshimi · Jun 28, 2017 at 01:55 PM
Of course:
// Include the namespace
using UnityEngine.PostProcessing;
// Assign the profile that you want to swap into
public PostProcessingProfile otherProfile;
void Update () {
// Whatever trigger you want
if(Input.GetKeyDown(KeyCode.F)) {
// Assign the profile
GetComponent<PostProcessingBehaviour>().profile = otherProfile;
}
}
@tanoshimi, when I play in editor and actually use this script, it gets this error: "NullReferenceException: Object reference not set to an instance of an object"
I'm just dragging these things in from the project folder. Is there another way to assign them?
This script is assu$$anonymous$$g that it is on (a component of) the camera which has your Post Processing Behaviour on it. If you're seeing that error, it might be because your script is on some other GameObject. In that case, your code should look like this:
using UnityEngine.PostProcessing;
// $$anonymous$$ake a variable to access your camera (don't forget to assign it)
public Camera camera;
public PostProcessingProfile otherProfile;
void Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)) {
// Assign the profile
camera.GetComponent<PostProcessingBehaviour>().profile = otherProfile;
}
}
Answer by AtGfx · Jun 28, 2017 at 11:51 AM
Can you just remove your behaviour and add the correct one when your player dies?
You can check on google how to do it to fit exactly your need, but you can use the Destroy function like Destroy(GetComponent(yourScript)).
With a bit more detail (this can be a solution, but not the only one) : - define a script that will handle the switch of your components - define your 2 script like you do before with your post-processing effects - when you detect that the player dies (in your first script) you Destroy the current component and add the correct one with AddComponent
You can have a look at AddComponent and Destroy documentation.
Try to make it work, and feel free to ask for details if it doesn't work ;)