IRaycastable Dialogue to Collider2D Dialogue

Okay I did fiddling with it all day until 2:21AM I think I have it but I’m missing something.

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(script == null)
            {
                return;
            }

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

That’s my code right now I know it has something to do with the GetComponent but I don’t know what I could call to fix it. To explain what I did I took out the Awake code since I think that was what’s throwing the null but I’m just trying to figure what to put in front of the Get component before I can fully test it all.

This is your issue. You are trying to get a PlayerConversant on the object that has the AIConversant. See the above comment.

Add this to your DreamersConvert script:

PlayerConversant playerConversant; // This will be a reference to the actual PlayerConversant

void Awake() //Let's get the PlayersConversant
{
     //The PlayerConversant is on the Player, not this GameObject, so we need to find the player
     //Then get the Player's PlayerConversant
     playerConversant = GameObject.FindWithTag("Player").GetComponent<PlayersConversant>();
}

In PlayerTalking, change

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

to

///We cached the playerConversant in Awake
playerConversant.StartDialogu(script);

Finally, nothing is calling PlayerTalking() in your script, so it’s never going to execute.

Change the name of the method to Update() and it will test ever frame.

Ya and when running the playerConversant threw a Debug.Log I get a Null return. So I don’t know if it’s something to do with my player or what but before you say make sure PlayersConversant is on the the player it is. but that’s the same issue on the Dialogue UI script it’s calling the PlayersConversant and saying the same Null error.

Do you have a playerConversant on the player?

As I said earlier

Are any other objects in the game tagged with the Player tag?

A quick way to check this, add this to the Awake in the script we’ve been working on:

foreach(GameObject go in GameObject.FindGameObjectsWithTag("Player")) //Edited from FindWithTag
{
    Debug.Log(go.name);
}

I got a new error and it won’t let me run the game.

Assets\Scripts\Scripts 2.0\Others\Dialogue\DreamersConvert.cs(17,39): error CS1579: foreach statement cannot operate on variables of type ‘GameObject’ because ‘GameObject’ does not contain a public instance definition for ‘GetEnumerator’

The Enumerator seams odd since in the Iraycast video the IEnumerator was deleted from the player script.

my mistake, GameObject.FindGameObjectsWithTag("Player), sorry about that.

playerCon = GameObject.FindGameObjectsWithTag(“Player”)

I’m hoping I understood that right but there it is in the void Awake part.

Assets\Scripts\Scripts 2.0\Others\Dialogue\DreamersConvert.cs(16,25): error CS0029: Cannot implicitly convert type ‘UnityEngine.GameObject’ to ‘Tree.Dialogue.PlayersConversant’

This is the error it’s throwing.

The foreach(GameObject go in FindGameObjectsWithTag("Player")) is to find all instances so you can see if there are extra GOs with “Player” on them. It’s just a quick and dirty way to list all of them. There should only be one output by that fragment of code.
To get the player (and it must be the only object tagged “Player” in the scene or it may not find the correct gameobject), you use GameObject.FindWithTag("Player)

Okay so I scrubbed the code double check tags…finally found it and I’m kicking my self for it at some point unknown when I swapped my test NPC tag to player it all works now.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms