0
\$\begingroup\$

I'm getting a NullReferenceException on OnTriggerStay. I've been going crazy trying to figure out why. I have tried to cut this down to only the relevent information. I got this code through a tutorial: https://www.youtube.com/watch?time_continue=48&v=_nRzoTzeyxU (just for credits).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class MainCharacter : MonoBehaviour {
    public int limbs;

    public float speed;
    public Vector3 jump;
    public float jumpForce;
    Rigidbody2D rb;
    public float fire_start_time = Time.time;
    Animator animator;
    public DialogueManager dialogueManager;

    // Checking if we are grounded
    public bool isGrounded;
    public LayerMask groundLayers;

    private SpriteRenderer myRenderer;

    public Sprite[] LimbSprites;

    void Start()
    {
        dialogueManager = gameObject.GetComponent<DialogueManager>();
    }

    public void OnTriggerStay2D(Collider2D collision)
    {
        if (Input.GetKey(KeyCode.Space))
        {
            Dialogue dialogue = collision.GetComponent<Dialogue>();
            dialogueManager.StartDialogue(dialogue);

        }
    }

}

Here is my dialogue manager:

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

public class DialogueManager : MonoBehaviour {

    public Text nameText;
    public Text dialogueText;

    public Animator animator;

    private Queue<string> sentences;

    // Use this for initialization
    void Start () {
        sentences = new Queue<string>();
    }

    public void StartDialogue (Dialogue dialogue) {

        animator.SetBool("IsOpen", true);

        nameText.text = dialogue.name;
        sentences.Clear();

        foreach (string sentence in dialogue.sentences) {
            sentences.Enqueue(sentence);
        }

        DisplayNextSentence();
    }

    public void DisplayNextSentence() {
        if (sentences.Count == 0) {
            EndDialogue();
            return;
        }

        string sentence = sentences.Dequeue();
        dialogueText.text = sentence;
    }

    void EndDialogue() {
        animator.SetBool("IsOpen", false);
    }
}

Here is DialogueTrigger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour {

    public Dialogue dialogue;

    public void TriggerDialogue () {
        FindObjectOfType<DialogueManager>().StartDialogue(dialogue);

    }
}

Here is Dialogue

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Dialogue : MonoBehaviour {

    public string name;

    [TextArea(3, 10)]
    public string[] sentences;

}
\$\endgroup\$
1
\$\begingroup\$

Here is your OnTriggerStay method

public void OnTriggerStay2D(Collider2D collision)
{
        if (Input.GetKey(KeyCode.Space))
        {
            Dialogue dialogue = collision.GetComponent<Dialogue>();
            dialogueManager.StartDialogue(dialogue);

        }
}

The problem is that you don't control with which object you are currently colliding, it can be any GameObject with a collider set on trigger and not just those who have a Dialog component.

I suggest adding a tag to those GameObjects or do a defensive check.

public void OnTriggerStay2D(Collider2D collision)
        {
            if (collision.gameObject.tag == "CanTalkWith")
            {
                if (Input.GetKey(KeyCode.Space))
                {
                    Dialogue dialogue = collision.gameObject.GetComponent<Dialogue>();
                    if (dialogue != null)
                    {
                        dialogueManager.StartDialogue(dialogue);
                    }

                }
            }
        }
\$\endgroup\$
  • \$\begingroup\$ Ah! Thanks so much :) This was driving me insane! You also taught me how to check if my character is on the ground haha. Much appreciated. It's all coming together \$\endgroup\$ – Jersey Fonseca Nov 26 '18 at 15:59
  • 1
    \$\begingroup\$ I recommend getting in the habit of using CompareTag() instead of .tag ==, as it plays with Unity's internals a little more efficiently, while accomplishing the same task just as legibly. \$\endgroup\$ – DMGregory Nov 26 '18 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.