I am trying to create objects and delete them each 50 frames. line objects have been created successfully but they never got destroyed! I even tried destroyimmediate() and still not working .. please help ... My code never worked like this ::
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
}