1
\$\begingroup\$

When I run my program I keep getting: MissingComponentException: There is no 'TextMesh' attached to the "Can" game object, but a script is trying to access it.Even though I have attached it as you can see:

enter image description here

In the script I am trying to edit the text of the Text Mesh like this¨:

private int count;
public TextMesh countText;

// Use this for initialization
void Start () {

    count = 0;
    counterText = gameObject.GetComponent<TextMesh>();
    counterText.text = "Count: " + count.ToString();

}

What I want is to count the cans that have been shot, so what I do next is:

  void OnCollisionEnter(Collider other)
{
    if (other.gameObject.CompareTag("Can"))
    {
        other.gameObject.SetActive(false);
        count = count + 1;
        counterText.text = "Count: " + count.ToString();
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

One thing I notice is that you have a public TextMesh property called countText that is populated in the inspector in your screenshot... but that's not what you appear to be using in the script.

Instead you seem to be using a different counterText variable which is initialized by getting a TextMesh component from the gameObject the Hit component is on (which it sounds like does not exist).

If your countText property is populated with the TextMesh you want to use like it seems, I would just remove the counterText = gameObject.GetComponent<TextMesh>(); line and switch all the others from counterText to countText, which should make them use the one that you apply in the inspector without issue.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .