2D Game Dialouge Box doesn't hide

Hello,

I created a dialouge system like in the following tutorial:

Everything worked fine until 20:28. That means that I have a working dialouge System, but the dialouge box is not hiding in the default state. A lot of people online posted that you can hide objetcs with e.g.

  • TheObject.SetActive(true),

but this doesn’t work in my project. So I followed a different tutorial:

and watched the time code “Animator”. You just make two animations, one for the hidden box set to defalut and one showing the box and implement it to the code. Since my code is different than the code in the second tutorial, I just implemented the three red lines from the second tutorial where I thought it would make sense, but I actually know nothing about C#, I’m still a complete noob. I could start the game, but the dialouge box was still not hidden, but this message appeared after starting the dialouge:

"UnassignedReferenceException: The variable animator of DialougeManager has not been assigned.
You probably need to assign the animator variable of the DialougeManager script in the inspector. "

So I draged the dialouge box UI Element to the Animator in the Inspector. Now the dialouge box is still shown in default and I get a nice zoom effect after starting the conversation (exidently did something good ^^), but the box is still shown at default. Do I have to assign something else to the inspector or do you know an easier way to hide the dialouge box (.enabled=false also didn’t work)?

Thanks for your time

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class DialougeManager : MonoBehaviour

{
    public Image actorImage;
    public TextMeshProUGUI actorName;
    public TextMeshProUGUI messageText;
    public Image backgroundBox;
[COLOR=#ff0000]    public Animator animator;[/COLOR]

    Message[] currentMessages;
    Actor[] currentActors;
    int activeMessage = 0;
    public static bool isActive = false;

    public void OpenDialouge(Message[] messages, Actor[] actors) {
        currentMessages = messages;
        currentActors = actors;
        activeMessage = 0;
        isActive = true;
        Debug.Log("Started conversation! Loaded messages: " + messages.Length);
        DisplayMessage();
      [COLOR=#ff4d4d]  animator.SetBool("IsOpen", true);[/COLOR]
      
    }
   
  


    void DisplayMessage() {
        Message messageToDisplay = currentMessages[activeMessage];
        messageText.text = messageToDisplay.message;

        Actor actorToDisplay = currentActors[messageToDisplay.actorId];
        actorName.text = actorToDisplay.name;
        actorImage.sprite = actorToDisplay.sprite;
    }

    public void NextMessage() {
        activeMessage++;
        if (activeMessage < currentMessages.Length) {
            DisplayMessage();   
        } else {
            Debug.Log("Conversation ended!");
            isActive = false;
            [COLOR=#ff0000]animator.SetBool("IsOpen", false);[/COLOR]
        }
    }

    // Start is called before the first frame update
    void Start()
    {
     
    }

    // Update is called once per frame
    void Update()
    {

   

        if (Input.GetKeyDown(KeyCode.Space) && isActive == true) {
            NextMessage();
        }


    }

   

}

When you get the above error, there’s only ONE answer, and it is ALWAYS the same.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

But how should I assign an Animator? It’s no variable. I think you don’t get my original problem. Why is my Dialouge Box still visible if I say it should be enabled = false. This works for game objects, but for a stupid reason not for UI elements, but the image is a png, idc if it’s UI or a game object.

And again, I fixed the null reference, but the dialouge box is still visible. Maybe I assigned it wrong.

“if I say it should be enabled == false” does not mean that code even ran, or that some other code didn’t run and turn it back on right afterwards.

Welcome to debugging! Here’s how to get started:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.