Locking the player movement and fading out when entering a trigger
Currently, I have two scripts. I have a FadeOut script (which fades the screen to black) and a TriggerFade script so the only time the FadeOut script works is when the player walks into the trigger.
I need help with the keeping the FadeOut script disabled until the player walks into the trigger.
As of right now, the game loads with the fade to black and after the player walks into the trigger the error message: "NullReferenceException: Object reference not set to an instance of an object TriggerFade.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/TriggerFade.cs:12)" pops up
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FadeOut : MonoBehaviour {
public Texture2D fadeOutTexture;
public float fadeSpeed = .8f;
private int drawDepth = 1000;
private float alpha = 1f;
private int fadeDir = -1;
void OnGUI()
{
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
GUI.depth = drawDepth;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}
public float BeginFade (int direction)
{
fadeDir = direction;
return (fadeSpeed);
}
void OnLevelWasLoaded()
{
BeginFade(-1);
}
}
that was my "FadeOut" script.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TriggerFade : MonoBehaviour {
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
FadeOut fadeOut = other.gameObject.GetComponent<FadeOut>();
fadeOut.enabled = true;
}
}
}
And this was my "TriggerFade script". I also don't want the player to move until the screen completely fades out, or let the player move but keep the screen fading no matter what (Until it stops and the player gets sent to the next scene)
I would also just like to say that I know there are a lot of questions like this, but I'm a beginner so it's still hard for me to look at a line of code and know how to alter it to my own needs. Sorry.
Your answer
![](https://koobas.hobune.stream/wayback/20220612141930im_/https://answers.unity.com/themes/thub/images/avi.jpg)