2D Interactive Sign -- Help.

Hey Gang,

I’ve still got my Unity/C# training wheels on and would love a little help. I’m trying to make an interactive sign, that when the character approaches it and commits an action (press Space), the sign will pop up or go away. Unfortunately, I’ve kind of thrown this together and I’m getting two Alerts:

  • error CS1061: ‘GameObject’ does not contain a definition for ‘Text’ and no accessible extension method ‘Text’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?)
  • the name ‘other’ does not exist in the current context.

I don’t think it’s a difficult fix, but I’m not sure where I should be re-defining my Text.
Any assistance would be appreciated.
Thanks!

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

public class Sign: MonoBehaviour
{

    public GameObject dialogBox;
    public Text dialogText;
    public GameObject text;
    public string dialog;
    public bool playerInRange;

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

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && playerInRange)
        {
            if (dialogBox.activeInHierarchy)
            {
                dialogBox.SetActive(false);
            }
            else
            {
                dialogBox.SetActive(true);
                dialogBox.text = dialog;
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            playerInRange = true;
        }
        if (other.CompareTag("Player"))
        {
            playerInRange = false;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (other.CompareTag("Player"))
        {
            playerInRange = false;
            dialogBox.SetActive(false);
            Debug.Log("Player Left Range");
        }
    }
}

As for the first problem, would be helpful to know which line exactly causes the problem, but seems like you are trying to access the text component in your gameobject instead of the Text object. (You didn’t even touch the dialogText component that you declared.)

For the second one, you should write other.gameObject.CompareTag()
instead of just other.comparetag

1 Like

Awesome thanks I’ll try this out! – I did just notice a few typos that popped up from me trying to highlight the specific issues. Just edited the code.

I think this was how I needed to do it. (I’m not getting an error now)

dialogBox.SetActive(true);
 dialogBox.GetComponent<Text>().text = dialog;

If it works, it works.
But don’t you have a Text dialogText to modify?