|
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 TowerCreate Script
(comments are locked)
|
|
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. Remember to type your variables too, I notice you haven't done that in the script you posted. Just as a reminder for faster performance! :-) Regarding the example: You could also have a script on the objects which moves them towards the player instead if you want a continuously endless game session.
Jul 18 '12 at 11:40 AM
save
Hey save! Thank you so much for your answer! I will be trying it out tonight, I just wanted to thank you in advance. I will have to study your script intently. :)
Jul 18 '12 at 11:35 PM
Geta-Ve
Awesome! Hope you don't feel like your previous work is for nothing, the way you did it is ok but not suitable for low-end devices. If you have repeating objects which travel out of scene view there's a good chance a reuse cycle is the best way to go. Check back with questions if they come up!
Jul 18 '12 at 11:44 PM
save
Furthermore, I just tried out the script as is, all I have to say is WOW, so much control. I definitely need to study this. Thank you so much!! EDIT: Also, no I don't feel I wasted my time with the previous script, I had learnt a lot piecing it together. and I think I am going to learn even more dissecting your script and seeing what makes it tick. :)
Jul 19 '12 at 12:02 AM
Geta-Ve
Actually, I guess I do have one question, off the top of my head. You mentioned that I should define the types for my variables, though I was under the impression that, at least in JavaScript, it wasn't really all that important, as JS detects automatically? A moment of thought would tell me that defining the variable type would simply give the game one less thing to calculate, thus speeding things up. Would I be correct in this assumption? :)
Jul 19 '12 at 12:14 AM
Geta-Ve
(comments are locked)
|
