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

UIFade not working

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

  1. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    Hi Im new to unity, I am trying to make a fade between scenes. It fades to black when I exit a scene, when I put the code "UIFade.instance" in AreaExit.cs. So that works but I get a error when putting the same code "UIFade.instance" into AreaEntrance.cs.
    So for some reason it recognises the code in AreaExit.cs but not in AreaEntrance.cs

    This is basic error I got:
    NullReferenceException: Object reference not set to an instance of an object
    AreaEntrance.Start () (at Assets/Scripts/AreaEntrance.cs:18)

    Here is my code:

    AreaExit:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class AreaExit : MonoBehaviour
    7. {
    8.  
    9.     public string areaToLoad;
    10.  
    11.     public string areaTransitionName;
    12.  
    13.     public AreaEntrance theEntrance;
    14.  
    15.     public float waitToLoad = 1f;
    16.     private bool shouldLoadAfterFade;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.          theEntrance.transitionName = areaTransitionName;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if(shouldLoadAfterFade)
    28.         {
    29.            waitToLoad -= Time.deltaTime;
    30.            if(waitToLoad <- 0)
    31.            {
    32.                   shouldLoadAfterFade = false;
    33.                   SceneManager.LoadScene(areaToLoad);
    34.            }
    35.         }
    36.     }
    37.  
    38.     private void OnTriggerEnter2D(Collider2D other)
    39.     {
    40.        if(other.tag == "Player")
    41.        {
    42.            //SceneManager.LoadScene(areaToLoad);
    43.            shouldLoadAfterFade = true;
    44.            UIFade.instance.FadeToBlack();
    45.  
    46.            PlayerController.instance.areaTransitionName = areaTransitionName;
    47.        }
    48.     }
    49. }
    50.  
    UIFade:
    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 static UIFade instance;
    10.  
    11.     public Image fadescreen;
    12.     public float fadeSpeed;
    13.  
    14.     public bool shouldFadeToBlack;
    15.     public bool shouldFadeFromBlack;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.        instance = this;
    21.  
    22.        DontDestroyOnLoad(gameObject);
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if(shouldFadeToBlack)
    29.         {
    30.             fadescreen.color = new Color(fadescreen.color.r, fadescreen.color.g, fadescreen.color.b, Mathf.MoveTowards(fadescreen.color.a, 1f, fadeSpeed * Time.deltaTime));
    31.  
    32.             if(fadescreen.color.a == 1f)
    33.             {
    34.                   shouldFadeToBlack = false;
    35.             }
    36.         }
    37.  
    38.         if(shouldFadeFromBlack)
    39.         {
    40.             fadescreen.color = new Color(fadescreen.color.r, fadescreen.color.g, fadescreen.color.b, Mathf.MoveTowards(fadescreen.color.a, 0f, fadeSpeed * Time.deltaTime));
    41.  
    42.              if(fadescreen.color.a == 0f)
    43.             {
    44.                   shouldFadeFromBlack = false;
    45.             }
    46.         }
    47.     }
    48.  
    49.     public void FadeToBlack()
    50.         {
    51.          shouldFadeToBlack = true;
    52.          shouldFadeFromBlack = false;
    53.         }
    54.  
    55.     public void FadeFromBlack()
    56.     {
    57.      shouldFadeToBlack = false;
    58.      shouldFadeFromBlack = true;
    59.     }
    60. }
    61.  
    62.  
    AreaEntrance:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AreaEntrance : MonoBehaviour
    6. {
    7.     public string transitionName;
    8.  
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         if(transitionName == PlayerController.instance.areaTransitionName)
    14.         {
    15.            PlayerController.instance.transform.position = transform.position;
    16.         }
    17.  
    18.         UIFade.instance.FadeFromBlack();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.     }
    26. }
    27.  
    Thanks for your help :)
     
    Last edited: Dec 31, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Didn't I just remind you of this like ten hours ago in the other post?

    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.
     
  3. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    possibility A: X (this line executes on different scripts)

    possibility B : maybe (when I take away the code line everything works...I think, I will check it tommorow

    possibility C: X (I dont think this posibility is valid to my script)

    possibility D: ? (Dont know what this means)

    possibility E: X

    possibility X: maybe (I hope not)

    Whay deos D mean?

    I REALLY appricate you helping everyone on the forum, I think alot of people are greatfull for that! You should be proud

    HAPPY NEW YEAR!!

    I hope you have time to give feedback :)
     
    Last edited: Dec 31, 2020
    seejayjames likes this.
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    You might also look at the stack trace: click the console message to see the calls that happened before the one it says in the error. Sometimes there's a problem earlier in the sequence.

    The main difference I see in the entrance and exit scripts is that you're trying to call UIFade in Start() in the Entrance one. It's probably not "born" yet ;) Call it after a brief delay (0.25 seconds is usually plenty) with an Invoke() and see if it works. This has fixed problems for me many times.

    Also, I'd re-work your bools in the UIFade script. If they always are opposite of each other, you only need one: shouldFadeToBlack. True? Fade to black. Else? Fade from black. And be sure to use if/else, you're using two ifs, which is wasteful as only one can be true in each Update(). For the setting functions, just have one and pass it the bool:
    public void setFadeStatus(bool arg) {}
     
  5. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    Im following a tutorial so I cant really customize the code too much. What do you mean with brief delay? Im new to unity
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class AreaEntrance : MonoBehaviour
    5. {
    6.     public string transitionName;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         if(transitionName == PlayerController.instance.areaTransitionName)
    11.         {
    12.            PlayerController.instance.transform.position = transform.position;
    13.         }
    14.         Invoke("callUIFade", 0.25f);
    15.     }
    16.     void callUIFade() {
    17.         UIFade.instance.FadeFromBlack();
    18.     }
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.     }
    23. }
     
  7. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    Thanks I will try this
     
  8. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    What deos invoke do?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Whoo boy, definitely learn how to pump the Unity online docs or you are going to have a really rough time! Google can answer that for you pronto-quicko.
     
  10. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    Yeah, just felt like asking
     
  11. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Funny tho. Most off the time those null exceptions will do nothing in compiled code, other than producing Debug.Log reports. However, I 100% agree with Dekker. Preventing null exceptors is the first thing to learn and maintain. What it does is basically just:

    Code (CSharp):
    1. if the object exists, please do something with it.
    You can't do anything to nothing, right?
     
  12. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Or it's like:
    You: "Do something with this (gameObject, Component)"
    Unity: "Which one? You never pointed me to it"
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Beg to differ!! All code after the exception is NOT executed. If you were contemplating 17 things to setup your game and the 13th one threw a null reference, numbers 14, 15, and 16 aren't gonna be run.
     
  14. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    But UIFade.instance works on the other scripts or maybe you meant another line of code?
     
  15. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    My visual studio is a bit broken so I when I click on the console nothing happens :/ I have tried to uninstall and redownload.
     
  16. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    No, was wondering if the Invoke() delay resolved the issue.
     
  17. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    I will test it later today :)
     
  18. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    It's a script execution order problem. You're setting you're UIFade Instance in Start, but also referencing it in Start in the two other scripts.

    Move the code from UIFade.Start() into UIFade.Awake() and you should be okay.
     
  19. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    UIFade.Start() UIFade.Awake, where did you get these codes from?

    I feel like I have 10 IQ lol
     
  20. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    IT WORKED! Thanks

    I followed a tutorial and he used the same code but how did it work for him but not for me? The course is 3 years old tho
     
  21. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Every MonoBehaviour script has an Awake() method/function, which is guaranteed to run before any Start method/function in any other MonoBehaviour script. So it is usual to setup Instance references in Awake and access them in Start or later on in the execution order.

    Using Invoke as you have in your start method is just holding back accessing the UIFade instance until (long) after it has been initialized. Although it works, it's not the best approach, and you should really try to get a handle on when to use Awake and Start and also probably how the whole script execution order works.
     
    Last edited: Jan 2, 2021
  22. Dogger-Frogger

    Dogger-Frogger

    Joined:
    Sep 16, 2018
    Posts:
    39
    But I dont have a UIFade Start() or a UIFade Awake()

    Edit: I made a void Awake in UIFade and puted "instance = this;" in it. Was that what you meant?

    I fooled around a bit, AreaEntrance code UIFade.instance.FadeFromBlack deosnt give a error anymore but it deosnt do anything
     
    Last edited: Jan 2, 2021