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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question 2D Game Dialouge Box doesn't hide

Discussion in 'Scripting' started by nniitchhandeleven, Dec 4, 2022.

  1. nniitchhandeleven

    nniitchhandeleven

    Joined:
    Feb 6, 2021
    Posts:
    3
    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.
    1. 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



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class DialougeManager : MonoBehaviour
    8.  
    9. {
    10.     public Image actorImage;
    11.     public TextMeshProUGUI actorName;
    12.     public TextMeshProUGUI messageText;
    13.     public Image backgroundBox;
    14. [COLOR=#ff0000]    public Animator animator;[/COLOR]
    15.  
    16.     Message[] currentMessages;
    17.     Actor[] currentActors;
    18.     int activeMessage = 0;
    19.     public static bool isActive = false;
    20.  
    21.     public void OpenDialouge(Message[] messages, Actor[] actors) {
    22.         currentMessages = messages;
    23.         currentActors = actors;
    24.         activeMessage = 0;
    25.         isActive = true;
    26.         Debug.Log("Started conversation! Loaded messages: " + messages.Length);
    27.         DisplayMessage();
    28.       [COLOR=#ff4d4d]  animator.SetBool("IsOpen", true);[/COLOR]
    29.      
    30.     }
    31.    
    32.  
    33.  
    34.  
    35.     void DisplayMessage() {
    36.         Message messageToDisplay = currentMessages[activeMessage];
    37.         messageText.text = messageToDisplay.message;
    38.  
    39.         Actor actorToDisplay = currentActors[messageToDisplay.actorId];
    40.         actorName.text = actorToDisplay.name;
    41.         actorImage.sprite = actorToDisplay.sprite;
    42.     }
    43.  
    44.     public void NextMessage() {
    45.         activeMessage++;
    46.         if (activeMessage < currentMessages.Length) {
    47.             DisplayMessage();  
    48.         } else {
    49.             Debug.Log("Conversation ended!");
    50.             isActive = false;
    51.             [COLOR=#ff0000]animator.SetBool("IsOpen", false);[/COLOR]
    52.         }
    53.     }
    54.  
    55.     // Start is called before the first frame update
    56.     void Start()
    57.     {
    58.      
    59.     }
    60.  
    61.     // Update is called once per frame
    62.     void Update()
    63.     {
    64.  
    65.    
    66.  
    67.         if (Input.GetKeyDown(KeyCode.Space) && isActive == true) {
    68.             NextMessage();
    69.         }
    70.  
    71.  
    72.     }
    73.  
    74.    
    75.  
    76. }
    77.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    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
     
  3. nniitchhandeleven

    nniitchhandeleven

    Joined:
    Feb 6, 2021
    Posts:
    3
    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.
     
  4. nniitchhandeleven

    nniitchhandeleven

    Joined:
    Feb 6, 2021
    Posts:
    3
    And again, I fixed the null reference, but the dialouge box is still visible. Maybe I assigned it wrong.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    "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://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

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