Hi I am new to unity and I have been stuck on this code for a while. I dont understand what causes the errors, there is a shouldFadeToBlack button and a shouldFadeFromBlack button.
But the I push shouldFadeToBlack this error comes up:
NullReferenceException: Object reference not set to an instance of an object
UIFade.Update () (at Assets/Scripts/UIFade.cs:26)
and when I push shouldFadeFromBlack this comes up:
NullReferenceException: Object reference not set to an instance of an object
UIFade.Update () (at Assets/Scripts/UIFade.cs:31)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIFade : MonoBehaviour
{
public Image fadescreen;
public float fadeSpeed;
public bool shouldFadeToBlack;
public bool shouldFadeFromBlack;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(shouldFadeToBlack)
{
fadescreen.color = new Color(fadescreen.color.r, fadescreen.color.g, fadescreen.color.b, Mathf.MoveTowards(fadescreen.color.a, 1f, fadeSpeed * Time.deltaTime));
}
if(shouldFadeFromBlack)
{
fadescreen.color = new Color(fadescreen.color.r, fadescreen.color.g, fadescreen.color.b, Mathf.MoveTowards(fadescreen.color.a, 1f, fadeSpeed * Time.deltaTime));
}
}
}
Thanks for your help