一旦玩家在 Unity 中捡起四枚硬币,无法弄清楚如何使用 SetActive(False) 让门消失

问题描述 投票:0回答:1

我正在制作 Roll-A-Ball 的 3D 再现,无论我采用何种方法,我都无法使用 SetActive 让门消失,这就是我现在所拥有的:

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

public class PlayerControls : MonoBehaviour
{
    Rigidbody rb;
    [SerializeField] float movementSpeed = 5f;
    [SerializeField] float jumpForce = 5f;
    int coins = 0;

    void Start()
    {
        rb = GetComponent<Rigidbody>(); 
    }

    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        rb.velocity = new Vector3(horizontalInput * movementSpeed, rb.velocity.y, verticalInput * movementSpeed);

         if (Input.GetButtonDown("Jump"))
        {
            rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Coin"))
        {
            Destroy(other.gameObject);
            coins++;
            Debug.Log("Coins: " + coins);
        }
                 
     if(other.gameObject.CompareTag("Door") && coins == 4)
         {
            
              gameObject.SetActive(false);
        
         }
    }
}

我用四个不同的部分制作了门(不确定这是否是最佳选择)并将我想要消失的部分标记为门。问题是,这种方法不仅不起作用,而且我也不想依赖 OnTriggerEnter,因为我不想触摸门才能打开。我希望门在硬币数为 4 时立即消失。Door setup

我一直在研究和尝试多种不同的方法,但没有一个奏效。我觉得这是一个我只是没有看到的如此简单的答案。
我试过在 void update 下为门添加一个 if 语句,但没有用。
我还尝试设置一个变量以在开始时找到 Door 游戏对象,然后放置一个 if 语句。

c# unity-game-engine gameobject
1个回答
0
投票

您没有引用要禁用的游戏对象:

if(other.gameObject.CompareTag("Door") && coins == 4){    
    // Missing the 'other'
    other.gameObject.SetActive(false);
       
}
© www.soinside.com 2019 - 2024. All rights reserved.