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

Help with dialog boxes

Discussion in 'Scripting' started by Ozenky, Apr 10, 2020.

  1. Ozenky

    Ozenky

    Joined:
    Apr 7, 2020
    Posts:
    6
    Hello everyone. I am pretty New to Unity, just started a few weeks ago, and I have been doing my best. Today I started by trying to make dialog boxes appear in the screen and found this vídeo
    and tried to implement myself this, with a few changes. The changes on the two scripts shown in the video are attached. There is one error popping up and I have no clue on what is wrong.

    I changed some variable names to spanish (my native language) for learning purposes, but then when it didn't work I did exactly as he said and it is also not working. My UNITY is 2018.4.20f1. Hope you can help me guys. Thanks.
     

    Attached Files:

  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    To note, most people don't want to download files to help you. You can post scripts directly in the forums with code tags.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Maestro : MonoBehaviour
    6. {
    7.  
    8.     public string texto;
    9.     private Mensajes Men;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         Men = FindObjectOfType<Mensajes>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        
    21.     }
    22.  
    23.     void OnTriggerStay2D (Collider2D other)
    24.     {
    25.         if(other.gameObject.name == "John")
    26.         {
    27.             if (Input.GetKeyUp(KeyCode.Return))
    28.             {
    29.                 Men.ShowBox(texto);
    30.             }
    31.         }
    32.     }
    33. }
    So, the answer is pretty straight forward. Something is null and it tells you which line of the script it's on. In this case it's the Men.ShowBox(texto) line

    Which means, that in Start when it tries to set a value, it can't find anything, so Men is null. Which means you have nothing in your scene with the Mensajes script on it or you have something with the script on it, but the object is inactive so it can't find it.

    I would suggest just making the variable public and dragging and dropping or using the SerializedField attribute to be able to drag and drop on it in the inspector instead if the Mensajes gameobject is going to be inactive.
     
    Ozenky likes this.
  3. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    Make it an interface or class to inherit the dialogue trigger from. This way, you can have each new class, implement the void, and do different things upon triggering.
     
  4. Ozenky

    Ozenky

    Joined:
    Apr 7, 2020
    Posts:
    6
    Two things: thanks for the advice on the posting, and thanks for the advice, it worked after I activated the object that was inactive.