Hello,
Is there a straight forward way to have a button Destroy() all instantiated game objects?
I have a List myList holding all GameObjects created.
I created a Button and a Script but I cant figure out how to pass in myList to it as a ref.
All the tutorials for buttons I’m finding are to just have it print out a debug. The most comprehensive button tutorial I went through had the button change scenes. I found another tutorial how to make a button destroy() a specific game object. Thats all great. But what I need does not translate to what I learned so far about buttons.
Im probably doing this all wrong.
So instead of pasting code, maybe I’ll stick to the goal…
How do I Destroy(allinstantiatedgameobjects) with a button click?
My program instantiates GameObjects randomly during play, so they aren’t things I can just click on in development and specify I want destroyed. I am basically looking for a reset button.
I can only do that (I as in what I know how to do) in my game controller script.
Here is what I tried. I created a button called ResetButton.
I then created a script for the button called ResetButtonTap.
Here is ResetButtonTap.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResetButtonTap : MonoBehaviour
{
public void ResetButtonTapped(ref List<GameObject> objList)
{
foreach (GameObject spot in objList)
{
Destroy(spot);
}
objList.Clear();
}
}
Thats about it. I dragged ResetButtonTap.cs into the On Click() area of the button.
I know I’m missing something here. I’m thinking I’m going about it the wrong way.
Why not reset it in the same class the list is in? You cannot pass a reference with a button unless you have the button call a method which then calls that method.