-1

This error happened after I closed the unity (I saved everything), the other day I opened it and the error came up. I tried to create prefabs and I was unable to use the debug log as mentioned in some topics.

    public void Show()
    {
        animator.Play("FadeInUI");
        
        cv.interactable = true;
        cv.blocksRaycasts = true;
    } 
    void Start()
    {
        isGameRunning = false;

        gameStartUI.Show();

    }

Error Here is the error

Everything is assigned as it was in the video and as it was the day before, I just opened it and the error appeared.

WillHiro
  • 3
  • 3
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yong Shun Mar 26 '21 at 00:01
  • It says the error occurred in your "GameUI" script at line 21. What do you have at line 21? – Simonster Mar 26 '21 at 00:03
  • ` public void Show() { **animator.Play("FadeInUI");** cv.interactable = true; cv.blocksRaycasts = true; }` – WillHiro Mar 26 '21 at 00:07
  • Looks like `animator`might not be assigned a variable. Where is it defined and do you assign an instance to it? – Pete Mar 26 '21 at 00:13
  • I just did a test, I deleted the objects from the hierarchy and created it again, the game ran as it should be, but as soon as I reopened the unity the error reappeared. – WillHiro Mar 26 '21 at 00:43
  • can you share a pic of your inspector for the object with the script on it? – Simonster Mar 26 '21 at 00:48
  • https://imgur.com/a/KEpAW49 – WillHiro Mar 26 '21 at 00:52
  • Can you share where and how you assign the animator variable? – impo Mar 26 '21 at 01:08
  • https://imgur.com/a/Z5r0vAD This? – WillHiro Mar 26 '21 at 01:14
  • I added the version of the project that I was learning in the video, and there is no such error when starting, but the scripts are the same. – WillHiro Mar 26 '21 at 01:39
  • 1
    I'm using 2019.4 and the tutorial version is 2019.2 Tomorrow I see the answers, I thank those who tried to help me. – WillHiro Mar 26 '21 at 01:51
  • It fails on line 21 because Animator reference is null. You grab your reference to the animator inside the start method. The only reason I can think of is that your Show method is called before Start when you do not have yet the reference, probably you call the Show method from some other place while the GameUI game object has been never active yet. Make sure you set the game object to be active before calling the Show method. – kolodi Mar 26 '21 at 12:14
  • How do I get the GameUI Script and activate it? – WillHiro Mar 26 '21 at 23:18
  • There's a checkbox in the component's inspector near the component name. If the checkbox is ticked off, the Start method wont run and your animator variable wont be assigned – NicknEma Mar 27 '21 at 20:30

1 Answers1

0

A possible explanation is that Unity is calling your GameController's Start() before your GameUI's Start();

One way to fix this is by moving the GetComponent call from the Start method to the Awake method. GetComponent calls (as well as any other reference-getting operation like GameObject.Find()) are usually done in Awake, as every Awake is called before any Start method. See this page for more info.

Another way would be to use this attribute to explicitly tell Unity which order to run your scripts in.

NicknEma
  • 414
  • 3
  • 13
  • Thank you very much, both worked. Would you know why the error only appeared in another version? (I know it has that compatibility in the new versions). But the versions are not far apart. – WillHiro Mar 29 '21 at 22:54