IRaycastable Dialogue to Collider2D Dialogue

So I watched the Dialogue system last night before bed and got me thinking at work after re reading the code in the IRaycastable could you get the same effect by making a collider2D trigger and tying it to a key like…when you talk to a bug in Hallow Knight for a example?

I only ask cause not everything is cut and dry I mean I did think of a hidden game object on the NPCs that holds this script and the collider but I think there would be problems with that…

Collider2D or Collider?

In HandleRaycast, you could check for Input.GetKeyDown(KeyCode.[keyname]) instead of checking for a mouse click.

Like a Box collider 2D where you use it as a kinda trigger point so it tells what goes with in it. I’m making a 2D game is why I’m asking and instead of raycast I didn’t know if something like a Trigger collider with a E key listener would work just as well as the script in the Iraycastable dialogue video.

Ah, gotcha… then the raycast is probably unneccessary for what you’re thinking… check out OnTriggerStay2D()

This fires continously while a trigger is happening, so I would check for the Player, and then test your Input.GetKeyDown in this method.

Huh didn’t think of the OnTriggerStay okay I’ll give it a look might be what I’m looking for then. But to be clear your basically saying I should still make a AIconversent script but instead of using Iraycast I should use the On Trigger stay code right?

okay so this is my code right now I had to // the Dialogue bit because it was throwing a error in my Update UI for some reason. But for some reason my intractable key isn’t calling it’s debug.log but everything is working.

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

namespace Tree.Dialogue
{

    public class DreamersConvert : MonoBehaviour
    {
        //[SerializeField] Dialogue script = null;
        [SerializeField]bool isPlayer = false;

#region Triggers
        void OnTriggerEnter2D(Collider2D other) 
        {
          if(other.tag == "Player") 
          {
            isPlayer = true;
            Debug.Log("Hello Player");
          }  
        }

        void OnTriggerExit2D(Collider2D other) 
        {
            if(other.tag == "Player")
            {
                isPlayer = false;
                Debug.Log("Goodbye Player");
            }
            
        }
#endregion

        void PlayerTalking()
        {
            if (Input.GetButtonDown("Interactive") && isPlayer){
                Debug.Log("We're cooking now!");
            }
        }
        
    }
}

I don’t really know what else to do but the bool is turning true and false as the player goes in and out of the trigger and I’ve also used isPlayer == true and still nothing.

Okay so I stayed up tell 2AM ramming my head into this not really seeing the simple answer it was missing a Update call for my PlayerTalking void I did my test and it works so I should be able to get the whole thing to work over the Raycast stuff but I’m still getting the weird Error involving the Update UI thing in the DialogueUI script.

The error reads

NullReferenceException: Object reference not set to an instance of an object
Tree.UI.DialogueUI.Start () (at Assets/Scripts/Scripts 2.0/Others/UI/DialogueUI.cs:24)

Don’t really know what’s going on but I think it has something with the added system where I’m letting the player pick when the scripted object Lines are played and not letting it do it, it’s self.

I don’t see anything calling PlayerTalking(). Is there more to the script I’m not seeing?

Ya I fixed that I added a Void Update last night that calls the void PlayerTalking it’s all working now.

1 Like

I do want to ask I’m getting a Reference Error and I don’t know if I’m doing something wrong or what but both are tied to the Dialogue code one being the UI update and the other is my call code. I don’t really know what’s going on since the Code its self isn’t throwing errors.

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

namespace Tree.Dialogue
{

    public class DreamersConvert : MonoBehaviour
    {
        [SerializeField] Dialogue script = null;
        [SerializeField] bool isPlayer = false;


         void Update()
        {
            PlayerTalking();
        }
        
#region Triggers
        void OnTriggerEnter2D(Collider2D other) 
        {
          if(other.tag == "Player") 
          {
            isPlayer = true;
            Debug.Log("Hello Player");
          }  
        }

        void OnTriggerExit2D(Collider2D other) 
        {
            if(other.tag == "Player")
            {
                isPlayer = false;
                Debug.Log("Goodbye Player");
            }
            
        }
#endregion

        void PlayerTalking()
        {
            if(script == null)
            {
                return;
            }

            if (Input.GetButtonDown("Interactive") && isPlayer)
            {
                Debug.Log("We got it Tim.");
                GetComponent<PlayersConversant>().StartDialoge(script);
            }
            return;
        }
    }
}

The error is my Update and the GetComponent are saying there’s errors then the DialogueUI Script is just the update code for the DialogueUI.

I’m guessing, because it’s still a bit vague… I like the specific lines marked…
Are you saying that the line

GetComponent<PlayerConversant>().StartDialoge(script);

is yielding the null reference?
If that’s the case, is this script on the PlayerConversant or on a UI?
The PlayerConversant is generally on the player, meaning that you’ll need a reference to the player, GetComponent will only look on the same GameObject it’s called from without a reference…

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

namespace Tree.Dialogue
{

    public class DreamersConvert : MonoBehaviour
    {
        [SerializeField] Dialogue script = null;
        [SerializeField] bool isPlayer = false;
        PlayerConversant playerConversant;

         void Awake()
         {
               playerConversant = GameObject.FindWithTag("Player").GetComponent<PlayerConversant>();
          }

         void Update()
        {
            PlayerTalking();
        }
        
#region Triggers
        void OnTriggerEnter2D(Collider2D other) 
        {
          if(other.tag == "Player") 
          {
            isPlayer = true;
            Debug.Log("Hello Player");
          }  
        }

        void OnTriggerExit2D(Collider2D other) 
        {
            if(other.tag == "Player")
            {
                isPlayer = false;
                Debug.Log("Goodbye Player");
            }
            
        }
#endregion

        void PlayerTalking()
        {
            if(script == null)
            {
                return;
            }

            if (Input.GetButtonDown("Interactive") && isPlayer)
            {
                Debug.Log("We got it Tim.");
                playerConversant.StartDialoge(script);
            }
            return;
        }
    }
}

The error is mainly a Reference error I think it has something to do with the null in the [SerializeField] Dialogue script = null; as for the DialogueUI script the error I’m getting with that is with the playersScript.onConverstaitonUpdate += UpdateUI; I’m going over the code but ya those are the errors fully both about a Reference error

If the script hasn’t been assigned in the inspector, then it will never get to the playerConversant.StartDialogue(script) line, meaning all that can be null there is the playerConversant, which you should now be getting in the Awake() method.

Dumb question but could it be as simple as a return error since in the playertalking I’m not returning true or false?

PlayerTalking is a void method, so you can’t even compile with true or false

Yes I know but I did digging and apparently a normal return just kinda stops everything so maybe if I some how turn my void into a public bool and return that true of false it should all work?..I don’t know I’m reading 5 different scripts seeing if I missed something in the Iraycastable video and comparing notes with my scripts, witch brought the idea of turning that void into a public bool or something that would allow me to return true or return false.

I only have ONE of your five scripts… so… I’m not in a position to answer this properly…

To explain the 5 other scripts I’m reading are the ones in the RPG Github all tying to my problem while I read over there’s I’m going over my script seeing if I missed stuff. That or if there’s something I could try like a public bool witch I think should help fix the null errors but I don’t know at this time I’m work shopping as I’m writing this out.

So I toyed around a bit found out the Null party is my player conversation list witch is being called in the code I shared here…trying to figure out why it’s being called Null.

Privacy & Terms