I have a scramble word game I am developing. There is a script mainly dealing with a function that instantiates a prefab. This prefab consists of a 2d square sprite of a fish and a TMP text as a child of it. The TMP text holds the unscrambled word that is loaded from a txt file when the prefab is instantiated.
There is another script that handles the movement of this prefab. So, given a velocity and speed, the fish with the attached word will move across the screen, and also destroys the instantiated prefab when it is out of bounds (i.e, the user was not able to guess it on time).
Another script handles user input.
My issue I am running into is when the user enters the return key or submits their word, I can't figure out how to "destroy" the correct instantiated prefab that the user is attempting to guess since there is usually multiple fish at once on the screen.
I've tried keeping a list of these game objects but I am not sure how to dynamically update it.
A bit broad of a question but here is what I would go for:
Have a list keeping track of your instances e.g. like
This way at any point in your app you can simply access and iterate the current existing/alive instances of
FishviaFish.Instances.Then you can get the specific instance vis the word like e.g.
In case the words a actually unique you could even rather go for a
Dictionary<string, Fish>instead.