【发布时间】:2023-03-14 08:34:01
【问题描述】:
我正在尝试创建对象并每 50 帧删除一次。线对象已成功创建,但它们从未被销毁!我什至尝试了 destroyimmediate() 仍然无法正常工作..请帮助...我的代码从来没有像这样工作过 ::
private int _currentInterval = 50;
private int _maxIntervalValue = 50;
private int i = 0;
// Update is called once per frame
void Update () {
if (_currentInterval == _maxIntervalValue)
{
float x = Random.Range(-10.0f, 10.0f), y = Random.Range(-10.0f, 10.0f);
DrawRay(i, new Vector3(0, 0, 0), new Vector3(x, 10, y));
i++;
_currentInterval--;
}
else if (_currentInterval <= 0)
{
Destroy(GameObject.Find("Ray_" + i));
_currentInterval = _maxIntervalValue;
}
else
_currentInterval--;
}
private void DrawRay(int ID, Vector3 StartPoint, Vector3 EndPoint)
{
#region Create Line
GameObject Ray = new GameObject();
Ray.transform.position = StartPoint;
Ray.AddComponent<LineRenderer>();
Ray.name = "Ray_" + ID;
LineRenderer lr = Ray.GetComponent<LineRenderer>();
lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
lr.SetColors(Color.red, Color.red);
lr.SetWidth(0.05f, 0.05f);
lr.SetPosition(0, StartPoint);
lr.SetPosition(1, EndPoint);
#endregion
}
【问题讨论】:
-
每帧 = 每帧?您想在同一帧中创建和销毁对象吗?如果是这种情况,那么您将看不到您创建的对象。请说明为什么需要这样做。也许有更好的方法来做到这一点
-
每 50 帧 .. 请看一下代码,更像是一个名为 currentInterval 的计数器,每 50 帧重置一次……问题出在破坏函数中。它从来没有工作过它应该是 :: Destroy(GameObject.Find("Ray" + i)); :: 所以我最终创建了很多没有删除功能的对象
-
您的标题显示每帧,正文显示 50 帧。我会同意你的 50 帧评论。但是现在,你的问题仍然没有意义。您想每 50 帧创建一个对象吗?你想什么时候摧毁它们?不知道什么时候很难提供帮助
-
对不起,我发布了我的旧代码,正如你所说的那样没有意义。我刚刚编辑它......好吧,一开始计数器将为=50,它将创建一个对象,然后它将一直减少到=0,那时我们刚刚创建的对象应该被删除...但事实并非如此...当计数器变为 0 时,destryiong 对象功能不起作用,并且在下一帧中创建了另一个新对象,而旧对象仍在其位置..我需要删除在创建新的之前的旧的