实例化克隆不会在碰撞进入时被破坏

时间:2020-07-09 05:26:08

标签: android unity3d

在击中后墙时试图摧毁玩家。从开始“球”入场时,它就可以很好地发挥作用。

但是它并没有破坏球的克隆,Debug.Log仍然像第一个时间一样运行,并且每次击中墙壁都被调用,但是为什么不破坏对象呢? ballClone它是一个预制件。

有什么建议吗?

public class BackWall : MonoBehaviour
{
public GameObject ball;



public static bool playerDestroyed = false;



public GameObject ballClone;



public void Spawn()
{
  GameObject playerclone = Instantiate(ballClone, new Vector3(-1.5f, 1.1f, -8f), 
Quaternion.identity);
    playerDestroyed = false;

    Destroy(ballClone, 10);


    StartCoroutine(waittoDestroy(7));
}

IEnumerator waittoDestroy(float time)
{
    yield return new WaitForSeconds(time);

    playerDestroyed = true;
}


public void OnCollisionEnter (Collision other)
{
    

    if (other.gameObject.tag == "Player")
    {

        Destroy(ball);
        Destroy(ballClone);

        
        
        playerDestroyed = true;

        Debug.Log("Ball should be destroyed");
    }

    
}

}

1 个答案:

答案 0 :(得分:1)

正如您所描述的,听起来好像您正在尝试销毁预制资产 ballClone

您想破坏的是此预制物的创建的实例playerclone

您可能应该存储该实例引用并使用例如

GameObject playerclone;

public void Spawn()
{
    playerclone = Instantiate(ballClone, new Vector3(-1.5f, 1.1f, -8f), Quaternion.identity);
    playerDestroyed = false;

    Destroy(playerclone, 10);  

    StartCoroutine(waittoDestroy(7));
}

IEnumerator waittoDestroy(float time)
{
    yield return new WaitForSeconds(time);

    playerDestroyed = true;
}  

public void OnCollisionEnter (Collision other)
{
    if (!other.gameObject.CompareTag ("Player")) return;
    
    Destroy(ball);
    Destroy(playerclone);       
        
    playerDestroyed = true;

    Debug.Log("Ball should be destroyed");
}
相关问题