Heyo! So this is the set up thus far.
That video, in case you don't want to watch shows one player character dodging towers is he flies through them (or, in the case of the code, as the towers fly by the player).
What I'd like to happen is that the INSTANCES of the tower prefab is destroyed either A) when an appropriate amount of time has passed, or B) when they hit a specific set of coordinates, ie. -50 and -50 on the Z and X axis (or something).
Currently I have the towers being spawned from one tower prefab at random intervals. So I figured, instead of detecting when the towers are past the cameras range and destroying them at that point, I would wait a predetermined amount of time, let's say 10 seconds, and then destroy them. I can then decrease the time limit as the difficulty increases (faster towers).
I, however, am stuck on a good way to destroy the towers. This is not to say that I am unable, I have already set up a solution where a script, attached to the tower prefab itself, runs every 10 seconds destroying said towers. This, however, ended up being very messy, I had no control over determining how many towers had been destroyed.
So, what I have attempted to do is integrate the destroy functionality in to my tower creation script, which is attached to a null/empty object at the center of the scene. And herein lies my issue.
For the life of me I am unable to destroy said towers, with any method that I have found, utilizing Destroy. My current attempt, as you will see below was me simply waiting for 10 seconds and then invoking Destroy on the newTower variable.
I will be honest, I have no clue if that was even SUPPOSED to work, but I tried it anyways. There are other solutions I have attempted but all have yielded no results. For example, amoung many other attempts, I tried GameObject.Find("tower_prefab") within Destroy, but that too was a bust.
So ya, I am at a total loss. It should be noted, that I am, obviously, hugely new to programming/unity, so I could very well be doing something insanely stupid! lol
You would benefit in reusing the objects instead of destroying and instantiating, that way it would also be optimized for mobile devices too. I cooked up a script which handles this:
Here's an example scene as UnityPackage. Feel free to ask if there's something in the script that seems unclear.