0

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
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Saajid Akram
  • 169
  • 4
  • 10
  • Per frames = every frame? You want to create and destroy object the-same frame? If this is the case then you won't see the object you created. Please clarify why you need to do this. Maybe there is a better way to do this – Programmer Jun 09 '18 at 04:25
  • Each 50 frames .. please have a look at the code, there is more like a counter called _currentInterval that gets reset each 50 frames ... the problem is in the destroying function. it never has worked is it supposed to be :: Destroy(GameObject.Find("Ray_" + i)); :: so I end up with lots of objects being created with no deleting function – Saajid Akram Jun 09 '18 at 04:37
  • Your title says per frame and the body says 50 frame. I will go with your 50 frames comment. But right now, your question still doesn't make sense. You want to create an Object every 50 frames? When do you want to destroy them? Without knowing when then it's hard to help – Programmer Jun 09 '18 at 04:43
  • I am sorry, I posted my old code which it doesn't it make sense as you said. I have just edit it ... ok 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 ... but that not what it happens ... when the counter get to 0 the destryiong object function doesn't work and in the next frame another new object gets created and the old one still in its place .. I need to delete the old one before the new one gets created – Saajid Akram Jun 09 '18 at 04:56

3 Answers3

3

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.

killer_mech
  • 1,568
  • 13
  • 26
2

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
1

I am new to unity and I don't know all the properties, but yes, if the object is destroyed it will give you error. So in my case I duplicated the prefab and I inserted it out of my scene, in that case I always took the duplicate prefab as a reference so it will never be destroyed because it will not be the gameobject of the instantiated class.

Maybe the answer for someone is trivial, but this should avoid any possible NullException if all instances of that requested object destroyed.

AlexPad
  • 10,364
  • 3
  • 38
  • 48