C# 算为已销毁但未真正销毁的游戏对象
我目前正在做一个突破式的游戏,当有超过5或6个球在游戏中时,我有一个销毁砖块的问题(没有实数重现这个错误,只是当有很多球在游戏中时才会发生)。有两个和三个球的力量,所以球的数量可以在很短的时间内迅速增加 在我的GameManager脚本中,我在场景开始时跟踪砖块的初始数量,每次球接触砖块时减去1。砖块数为零后,玩家获胜,游戏结束。这个游戏只需要少量的球,但是当球太多的时候,这个过程似乎就失败了 GameManager.cs的相关代码:C# 算为已销毁但未真正销毁的游戏对象,c#,unity3d,game-physics,C#,Unity3d,Game Physics,我目前正在做一个突破式的游戏,当有超过5或6个球在游戏中时,我有一个销毁砖块的问题(没有实数重现这个错误,只是当有很多球在游戏中时才会发生)。有两个和三个球的力量,所以球的数量可以在很短的时间内迅速增加 在我的GameManager脚本中,我在场景开始时跟踪砖块的初始数量,每次球接触砖块时减去1。砖块数为零后,玩家获胜,游戏结束。这个游戏只需要少量的球,但是当球太多的时候,这个过程似乎就失败了 GameManager.cs的相关代码: void Start() { livesText.t
void Start()
{
livesText.text = "Lives: " + Lives;
numOfBalls = GameObject.FindGameObjectsWithTag("Ball").Length;
numOfBricks = GameObject.FindGameObjectsWithTag("Brick").Length;
//Debug.Log(numOfBalls);
}
public void UpdateBrickNumber()
{
numOfBricks--;
if(numOfBricks <= 0)
{
numOfBricks = 0;
GameOver();
}
}
void GameOver()
{
gameOver = true;
if(numOfBricks == 0)
{
WinPanel.SetActive(true);
}
else
{
GameOverPanel.SetActive(true);
}
}
...
void Start()
{
livesText.text=“生活:”+生活;
numobolls=GameObject.FindGameObjectsWithTag(“球”).Length;
numobricks=GameObject.FindGameObjectsWithTag(“Brick”).Length;
//Debug.Log(numoball);
}
public void UpdateBrickNumber()
{
numOfBricks--;
如果(numOfBricks=1&&rand2感觉在一帧中发生了多个碰撞,导致砖块数低于剩余砖块数,则应解决此问题:
void OnCollisionEnter2D(Collision2D collision)
{ //Check if the brick has already been processed
if (collision.transform.CompareTag("Brick") && collision.collider.enabled)
{
Debug.Log("Brick Hit");
//Chance of powerup to spawn
int rand = Random.Range(1, 101);
//Chance of each power up
int rand2 = Random.Range(1, 101);
if (rand < 7)
{
if (rand2 >=1 && rand2<=20) {
Instantiate(SpeedBall, collision.transform.position, collision.transform.rotation);
//Debug.Log(" Speed Power Up Created ");
}
...
Rest of power ups
...
}
Transform newExplosion = Instantiate(explosion, collision.transform.position, collision.transform.rotation);
Destroy(newExplosion.gameObject, 2f);
gm.UpdateBrickNumber();
//Mark the brick as hit already so other balls can't hit it!
collision.collider.enabled = false;
Destroy(collision.gameObject);
}
}
无效OnCollisionInter2D(碰撞2D碰撞)
{//检查砖块是否已处理
if(collision.transform.CompareTag(“砖”)和&collision.collider.enabled)
{
Log(“砖块命中”);
//通电产卵的机会
int rand=随机范围(1011);
//每次通电的机会
int rand2=随机范围(1011);
如果(兰特<7)
{
如果(rand2>=1和&rand2快速简短回答
在C#和Java中,对象不是立即销毁的,而是放在一个要删除的过程中,有时发生在中间,有时不发生
有时,其他对象仍然引用它们。解决方法是在之前删除这些引用
void OnCollisionEnter2D(Collision2D collision)
{
...
Transform newExplosion = Instantiate(explosion, collision.transform.position, collision.transform.rotation);
// notify other objects that reference "newExplosion.gameObject"
Destroy(newExplosion.gameObject, 2f);
newExplosion.gameObject = null;
gm.UpdateBrickNumber();
// notify other objects that reference "collision.gameObject"
Destroy(collision.gameObject);
collision.gameObject = null;
...
} // void OnCollisionEnter2D(...)
干杯。您没有说明实际的问题是什么。错误:“无法将Property collision.enabled分配给--它是只读的”将其位置更改为屏幕外是否会产生相同的效果,或者是否有其他方法可以使用已启用的function@LarrBear抱歉,已修复。您想禁用碰撞器,但我无意中写入collision@LarrBear回答你的问题,不,我不相信改变立场就能解决问题。如果我没记错的话,Unity engine Physical phase计算所有碰撞,将它们放入列表中,然后在碰撞阶段对它们进行处理。这意味着即使更改位置,碰撞也已被检测到,因此仍将调用OnCollision
函数。(~70%确定)@LarrBear这可能会有帮助。注意内部物理阶段和oncollision之间的差距!另外,oncollision和ondestroy之间的巨大差距!谢谢!最初它没有太大区别,但我在实例化和其他函数之前将其作为if循环的第一行,现在它工作得很好!错误:“Property collision.gameObject无法分配到--它是只读的”与newExplosion.gameObject相同。这与C#或Java无关,Destroy
是一个特定于Unity的函数,实际的销毁是有时间的。请参阅我的答案+文档
void OnCollisionEnter2D(Collision2D collision)
{
...
Transform newExplosion = Instantiate(explosion, collision.transform.position, collision.transform.rotation);
// notify other objects that reference "newExplosion.gameObject"
Destroy(newExplosion.gameObject, 2f);
newExplosion.gameObject = null;
gm.UpdateBrickNumber();
// notify other objects that reference "collision.gameObject"
Destroy(collision.gameObject);
collision.gameObject = null;
...
} // void OnCollisionEnter2D(...)