简体   繁体   中英

Creating and destroying game objects each certain number of frames in Unity

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
}

Just make your counter Destroy(GameObject.Find("Ray_" + (i-1))); . Right now the GameObject.Find it is trying to find is one count above your original value which doesn't exist at that moment. This should solve your problem of object not getting destroyed.

At the beginning the counter will be =50 and it will create an object then it will be reduced all the way to =0 and at that moment the object that we just created is supposed to be deleted

1 .When you instantiate an object like this GameObject Ray = new GameObject(); you need to make the GameObject Ray variable a global variable so that you can access it and destroy it later on. This is better than looking for it with GameObject.Find to destroy it.

2 .You don't need most variables in your code. What you are doing can be simplified with Time.frameCount by checking if Time.frameCount % 50 is 0 .

3 .Don't name your variable Ray because there is a Unity API that name.

A simplified version of code for what you're tryingto do. DrawRay is removed but you can add that.

GameObject obj = null;

void Start()
{
    //Create new one
    obj = new GameObject();
}

void Update()
{
    //Check if 50 frames has passed
    if (Time.frameCount % 50 == 0)
    {
        //Destroy old one
        if (obj != null)
            DestroyImmediate(obj);

        //Create new one again
        obj = new GameObject();
    }
}

Don't use that code in your DrawRay function. There will be a memory leak in your code each time you create new Material . Use Object Pooling to re-use the objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM