Search Unity

Player not being recognized by Colliders?

Discussion in 'Scripting' started by faire4, Jun 25, 2019.

  1. faire4

    faire4

    Joined:
    Jun 5, 2018
    Posts:
    2
    I'm making a 2D RPG and I'm working on the Dialogue system right now, I have an NPC and it has 2, 2D Box Colliders. One is a "is trigger" so the player can go through and activate the system, the other is a box collider so the player can't go through the NPC. Whenever the player enters the "is trigger area" it doesn't notice the player so it can't activate the dialogue system. I have 2 scripts a DialogManager and a DialogActivator. Not sure why it's doing that if anyone can help?

    DialogManager:

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

    public class DialogManager : MonoBehaviour
    {

    public Text dialogText;
    public Text nameText;
    public GameObject dialogBox;
    public GameObject nameBox;

    public string[] dialogLines;

    public int currentLine;

    public static DialogManager instance;
    private bool justStarted;

    // Start is called before the first frame update
    void Start()
    {
    instance = this;

    //dialogText.text = dialogLines[currentLine];

    }

    // Update is called once per frame
    void Update()
    {

    if(dialogBox.activeInHierarchy)
    {
    if(Input.GetButtonUp("Fire1"))
    {
    if (!justStarted)
    {

    currentLine++;

    if (currentLine >= dialogLines.Length)
    {
    dialogBox.SetActive(false);
    }
    else
    {
    dialogText.text = dialogLines[currentLine];
    }
    } else
    {
    justStarted = false;
    }
    }
    }

    }

    public void ShowDialog(string[] newLines)
    {
    dialogLines = newLines;

    currentLine = 0;

    dialogText.text = dialogLines[0];
    dialogBox.SetActive(true);

    justStarted = true;
    }
    }



    DialogActivator:

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

    public class DialogActivator : MonoBehaviour
    {

    public string[] lines;

    private bool canActivate;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    if(canActivate && Input.GetButtonDown("Fire1") && !DialogManager.instance.dialogBox.activeInHierarchy)
    {
    DialogManager.instance.ShowDialog(lines);
    }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
    if(other.tag == "Player")
    {
    canActivate = true;
    }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
    if (other.tag == "Player")
    {
    canActivate = false;
    }
    }
    }

     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    - check if you have a Rigidbody2D on the player
    - check if you have a 2D collider of some kind on the player
    - put Debug.Log() statements inside your trigger messages to print what the tag is
     
  3. faire4

    faire4

    Joined:
    Jun 5, 2018
    Posts:
    2
    attached is the player prefab:

    upload_2019-6-25_11-9-18.png

    So i have both a rigidbody2d and a circle collider 2d but still it's not detecting anything.