【问题标题】:Instantiate clone not being destroyed on Collision enter实例化克隆在碰撞输入时不被破坏
【发布时间】:2020-07-09 14:01:01
【问题描述】:

在击中后墙时试图摧毁玩家。它适用于玩家从“球”开始就在场景中

但它不会破坏球的克隆,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");
    }

    
}

}

【问题讨论】:

    标签: android unity3d


    【解决方案1】:

    正如你所描述的,听起来你正试图破坏 预制资产 ballClone!

    你宁愿破坏的是这个预制的创建Instanceplayerclone

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

    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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多