Unity 游戏对象可见性 - 无法多次显示游戏对象

Unity Game Object Visibility - Unable to display Game Object more than once

我有一个 Canvas 子对象,其中有一个 Image 和一个 TextMeshPro (TMP),对话框的 canvas 组件在 Start() 方法中设置为 false 以便隐藏它主要 Canvas。 TMP 出现在图像上(就像对话框中的文本)。我在 2D 环境中有一个玩家和一个硬币精灵。当玩家拿起硬币时,我尝试显示如下所示的对话框和TMP。

public class PlayerMovement : MonoBehaviour {

    public GameObject suggestion;
    public GameObject dialogBox;

    private bool wasSuggestionShown; //to check if dialog was shown

    private void Start()
    {
        wasSuggestionShown = false;
        suggestionTimer = 0;
        dialogBox.GetComponent<Canvas>().enabled = false; //To hide the dialog box
    }

    void Update () {

        //horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
        horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input

        if (wasSuggestionShown)
        {
            suggestionTimer += Time.deltaTime;
            if (suggestionTimer > 5)
            {
                wasSuggestionShown = false;
                dialogBox.GetComponent<Canvas>().enabled = false; //TO hide dialog box after displaying it
            }
        }
    }

    void FixedUpdate ()
    {
        // Move the character
        controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
        jump = false;
    }


    //For destroying coin object on collision with player
    private void OnTriggerEnter2D(Collider2D col)
    {
        
        if (col.gameObject.CompareTag("coin"))
        {

            //For destroying coin and to recude player movement speed.

            Destroy(col.gameObject); //Coin Disappears

            isRunSpeedReduced = true;
            runSpeed = 10f;

            //For Showing dialog box

            dialogBox.GetComponent<Canvas>().enabled = true;
            wasSuggestionShown = true;
        }
    }
}

OnTriggerEnter2D()方法中,我检查玩家角色是否接触到硬币,如果是,我销毁对象并显示对话框和TMP,并在5 秒后隐藏它们。问题是

"当我包含另一个硬币时,相同的对话框和 TMP,当玩家选择第二个硬币时不会出现。两个硬币具有相同的标签 'coin'"

有人可能会争辩说,如果脚本在同一个被破坏或不活动的对象中,那么这是不可能的。但我是在附加到玩家对象的玩家移动脚本中完成所有这些操作的。

另外,我切换对话框的方式并没有什么不同。无论是 dialogBox.GetComponent<Canvas>().enabled = true; 还是 dialogBox.SetActive(true) 这些都只显示一次,而且是第一次出现。

即使我想实例化它,我也不知道要在 canvas 中正确定位它的确切转换。 (我想要它在底部中间部分,就像它可以锚定一样)

场景层次结构:

问题出在您的 Update(),您在 suggestionTimer 结束后关闭了 canvas:

void Update () {

    //horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
    horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input

    if (wasSuggestionShown)
    {
        suggestionTimer += Time.deltaTime;
        if (suggestionTimer > 5)
        {
            wasSuggestionShown = false;
            dialogBox.GetComponent<Canvas>().enabled = false; //TO hide dialog box after displaying it
        }
    }
}

原因是您在击中新硬币时从未重置 suggestionTimer。这样做:

private void OnTriggerEnter2D(Collider2D col)
{
    
    if (col.gameObject.CompareTag("coin"))
    {

        //For destroying coin and to recude player movement speed.

        Destroy(col.gameObject); //Coin Disappears

        isRunSpeedReduced = true;
        runSpeed = 10f;

        //For Showing dialog box

        dialogBox.GetComponent<Canvas>().enabled = true;
        wasSuggestionShown = true;

        //  !!! ADD THIS
        suggestionTimer = 0;
    }
}

问题是,在 OnTriggerEnter2D 上,您设置了 wasSuggestionShown = true,然后在下一个 Update 方法中,您正在检查 wasSuggestionShown,看到它是真的,然后转向canvas 马上又关机了。

这可能会有所帮助。这只是十几种方法中的一种,可以提供帮助:

private bool _showSuggestion = true;
public bool showSuggestion
{
  get { return _showSuggestion; }
  set   
  {
    if ( !value && _showSuggestion )
    {
      dialogBox.SetActive ( false );
      _showSuggestion = value;
    }
  }
}


void Update () {

    //horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
    horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input

    if ( showSuggestion )
    {
        suggestionTimer += Time.deltaTime;
        if ( suggestionTimer > 5f ) showSuggestion = false;
    }
}

上面的代码将等到您第一次将 showSuggestion 设置为 false 时,然后忽略任何进一步打开建议的尝试。我怀疑这就是你想要的?如果我看错了情况,可以稍微调整一下代码以获得正确的行为。