Home>

If i attach a Box Collider to a GameObject tagged with "memo" and hit a player (first-person player) with a expanded GameObject (like Figure 1), the text 1 text (set in public) will be written. I want to display, but for some reason the characters are not displayed. (Figs. 2 and 3)
[Figure 1]

[Figure 2]

I want to do this. (The yellow one is a GameObject tagged with memo)
[Fig. 3]

However, the text does not come out even if I touch it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Game: MonoBehaviour
{
    public Text text1;
    public static bool supermarketkey = false;
    public static bool buildingkey = false;
    public static bool shopopen = false;
    public GameObject Player;
    // Start is called before the first frame update
    void Start ()
    {
    }
    // Update is called once per frame
    void Update ()
    {
    }
    private void OnTriggerEnter (Collider other)
    {

            if (other.gameObject.tag == "chest")
            {
                text1.text = "The supermarket key came out of the box";
                supermarketkey = true;
            }
        else if (other.gameObject.tag == "shop in")
        {
            if (! supermarketkey)
            {
                text1.text = "It looks like you need a key.";
            }
            else if (supermarketkey)
            {
                SceneManager.LoadScene ("Shop Scene");
            }
        }
            else if (other.gameObject.tag == "chest2")
        {
            text1.text = "The building key came out of the box";
            buildingkey = true;
        }
            else if (other.gameObject.tag == "shop out")
        {
            if (! shopopen)
            {
                shopopen = true;
            }
                geta ();
                SceneManager.LoadScene ("GameScene");
        }
        else if (other.gameObject.tag == "memo")
        {
            text1.text = "The memo is missing. It says" 3 t e m k e t @ e shift0 s;". "
                ;
        }
    }
    public static bool geta ()
    {
        return shop open;
    }
}

I looked it up, but I couldn't find any search results. The word "Unity" touches but ONTriggerEnter does not start "",[Searched URL]

Unity version 2019.3.14f1

  • Answer # 1

    If you can't find the cause, be honestDebug.logIt is good to check with. More than half of Unity's errors are pretty much this silly way.

    For example
    1 Check if OnTriggerEnter itself is running by outputting it with the contact target in the debug log.
    → If ❌ here, the contact judgment itself cannot be taken in the first place
    For example, physics is not working because there is no Rigidbody on either the touching side or the touching side.

    2 Next, check if the judgment that touched the memo is taken
    → If ❌ here, the collision detection only for the memo is strange (tag is removed, collider's mistake, etc.)

    3 Next, check that the text is properly referenced.
    → Here, if ❌, you can see that the collision detection is working, so you can simply narrow down to mistakes on the text side.

    private void OnTriggerEnter (Collider other)
        {
                Debug.Log ("On Trigger Enter:" + other.gameObject.name);
                if (other.gameObject.tag == "chest")
                {
                    text1.text = "The supermarket key came out of the box";
                    supermarketkey = true;
                }
            else if (other.gameObject.tag == "shop in")
            {
                if (! supermarketkey)
                {
                    text1.text = "It looks like you need a key.";
                }
                else if (supermarketkey)
                {
                    SceneManager.LoadScene ("Shop Scene");
                }
            }
                else if (other.gameObject.tag == "chest2")
            {
                text1.text = "The building key came out of the box";
                buildingkey = true;
            }
                else if (other.gameObject.tag == "shop out")
            {
                if (! shopopen)
                {
                    shopopen = true;
                }
                    geta ();
                    SceneManager.LoadScene ("GameScene");
            }
            else if (other.gameObject.tag == "memo")
            {
                Debug.Log ("contact memo");
                Debug.Log ("Text reference check:" + text1);
                text1.text = "The memo is missing. It says" 3 t e m k e t @ e shift0 s;". "
                    ;
            }
        }