Home > Enterprise >  How to delete all prefabs when an event happens in unity
How to delete all prefabs when an event happens in unity

Time:10-25

Extremely new to unity and c# after switching across from Python. Once all the balls are scored in my game I want all the 'blockers' (prefabs which I have instantiated) to be removed from the screen and a new set to be spawned randomly on screen in random positions. The blocker prefabs spawn randomly when all balls are scored, however the old prefabs, besides one which deletes each time, stay on screen rather than deleting. I have tried looping through the blockers in the code below to delete and I think this is where the issue is as only one gameobject deletes at this stage:

   if (SpawnManager.tempclonecount == 0 )
   {     
       for (int i = 0; i < SpawnManager.blockeramounts; i  )
            {
                Destroy(gameObject);
                
            }
            SpawnManager.tempclonecount = 1;
   }

SpawnManager is an empty object which I have used to spawn objects onto screen, tempclonecount a variable stating when the old game objects should be removed from the game. This part of the code works well. blockeramounts is the number of prefabs initially on screen and I hoped looping through the number of prefabs would delete all of the prefabs. It only deletes one. How do I change this? Here is the code for creating the blockers in spawnmanager also, if helpful:

     void Update()
     { 
        int blockeramount = Random.Range(2, 7);
        blockeramounts = blockeramount;
        for (int i = 0; i < blockeramount; i  )
          {
             int blockerindex = Random.Range(0, blockerPrefabs.Length);
             Instantiate(blockerPrefabs[blockerindex], new Vector3(Random.Range(-30, 30), 0, Random.Range(-30, 30)), blockerPrefabs[blockerindex].transform.rotation);
            
          }
     }

CodePudding user response:

While reading your description of the problem I got a bit confused by some of the terms you used. That's all fine since you are new to Unity and still learning. What i managed to figure out is that you have a SpawnManager script attached to an empty game object which instantiates your blocker prefabs. Then in another script you are getting the SpawnManager refernce and checking if you should destroy the current ones and instantiate a new set.

First of all what i would do is, after instantiating an object, to store it in an array or a list

...
public List<GameObject> blockers = new List<GameObject>();
...
void Start()
{
    ...
}

void Update()
{
    int blockeramount = Random.Range(2, 7);
    blockeramounts = blockeramount;

    for (int i = 0; i < blockeramount; i  )
      {
         int blockerindex = Random.Range(0, blockerPrefabs.Length);
         var blocker = Instantiate(blockerPrefabs[blockerindex], new Vector3(Random.Range(-30, 30), 0, Random.Range(-30, 30)), blockerPrefabs[blockerindex].transform.rotation);
         blockers.Add(blocker);
      }
}

After which i would add a new method which does the check to see how many remaining blockers there are. This method should go inside SpawnManager.

public void CheckAndDeleteBlockers()
{
    if (tempclonecount == 0 )
    {     
       foreach(var blocker in blockers)
        {
            blockers.Remove(blocker);
            Destroy(blocker);
        }
    }
}

And you should call it from the other script with:

...
public SpawnManager spawnManager;
...
void Start()
{
    spawnManager = FindOjectOfType<SpawnManager>();
}
//for example
void Update()
{
    spawnManager.CheckAndDeleteBlockers();
}

I understand the way you are trying to do this, but let's say that this isn't the correct way. I would suggest that you look up what object pooling is.

NOTE: The creator of the pooling tutorial that I mentioned above is a great source for Unity and C# beginners, so I would recommend that you watch some of his other videos. Good luck in learning Unity.

CodePudding user response:

What I believe is causing your issue, is that your are only specifying one gameObject to be destroyed. You need to call Destroy() on each blocker you instantiated.

Hope my advice helps.

  • Related