Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Object not set as an intance of an object. Line 26 and 31

Discussion in 'Scripting' started by Dogger-Frogger, Dec 31, 2020.

  1. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    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)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class UIFade : MonoBehaviour
    7. {
    8.  
    9.     public Image fadescreen;
    10.     public float fadeSpeed;
    11.  
    12.     public bool shouldFadeToBlack;
    13.     public bool shouldFadeFromBlack;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.      
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(shouldFadeToBlack)
    25.         {
    26.             fadescreen.color = new Color(fadescreen.color.r, fadescreen.color.g, fadescreen.color.b, Mathf.MoveTowards(fadescreen.color.a, 1f, fadeSpeed * Time.deltaTime));
    27.         }
    28.  
    29.         if(shouldFadeFromBlack)
    30.         {
    31.              fadescreen.color = new Color(fadescreen.color.r, fadescreen.color.g, fadescreen.color.b, Mathf.MoveTowards(fadescreen.color.a, 1f, fadeSpeed * Time.deltaTime));
    32.         }
    33.     }
    34. }
    35.  
    36.  
    Thanks for your help :)
     
  2. payalzariya07

    payalzariya07

    Joined:
    Oct 5, 2018
    Posts:
    85
    Check out the reference of fadescreen image in inspector
     
    Bunny83 and Dogger-Frogger like this.
  3. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    Thanks it worked! Cull Transparent Mes was off
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Glad you're up and running again, but remember: The answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    Bunny83 likes this.