There is a prefab on which the script hangs with the generation of id for each created object. When starting the scene, 20 objects with id are generated (1,2,3 ... 20). Purpose: to get a specific object by id (from a script). This is for removal. The question is: can this be done, if so, how? C #
2 answers
In the script, have a dictionary, where the key is the identifier, and the value is the generated object.
Dictionary<int, GameObject> myObjects; At instantiation we add there
var myObj = Instantiate(prefab, new Vector3(2.0f, 0, 0), Quaternion.identity); myObjects.Add(1, myObj); Further, when you need to delete by id, you will turn to the dictionary and remove it from there and from the scene.
var objToRemove = myObjects[1]; dic.RemoveAt(1); Destroy(objToRemove); It seems something like that.
If you have an instance of the script
instance.gameObject.Destroy(); You can also make a public static List with your objects. And inside the object already have an ID.
Then it is enough just to find in this sheet through Linq an object with the necessary ID and destroy it.
To do this through some external dikshinari is somehow a crutch, therefore I do not advise it. It is cussy because it is illogical from the point of view of the PLO to store the parameter of the object outside the object.