- Home /
Instantiate a object after destroying a instantiated object.
Hello you all!
I am working on a snake alike game, just to get the feel for game development!
I have gotten a kind of apple spawn at a random location every time the game starts. It workes great, but when i destroy the object i collide with (apple) so it kinda picks it up or eats it, after that i dont know how to instantiate ONLY one more apple at a random location, and then repeat the proces.
Can you please help me and i will be greatful :)
The simple answer is to simply instantiate once at a random location like you did before. If you include some more description and/or some of your code, it would be a lot easier to provide a clearer answer to your specific use case.
$$anonymous$$y code is:
var shrooms : Transform;
function Start () { var position = Vector3(Random.Range(-36,36),Random.Range(-1.517441, -1.517441), Random.Range(-23, 23)); Instantiate(shrooms, position, Quaternion.identity); }
Now i dont know how to instantiate it ONCE more and reapeat the process after that. The closet i have come to a soloution reasulted in hundreds of thousends (allmost) instantiated objecte.
Answer by Justin Warner · Dec 06, 2010 at 07:08 PM
So:
function Update () { var appleTarget = GameObject.FindWithTag("Apple");
if( appleTarget == null ) { SpawnOneApple(); }
}
function SpawnOneApple() { //Put code for spawning an apple here... }
This'll always make sure one apple is on the board, because if one is gone, it spawns one, but if their is one their, then appleTarget isn't null...
Hope that helps =).
Hmm.. it looks like it should work, and a do understand the code but unfortunetly it dose not work. But thanks :)
What doesn't work? Doesnt any apples spawn? Are apples spawning like crazy?
You might have to put the var appleTarget = ... outside of the function... and it should work if you put in your spawning code...
Your answer
Follow this Question
Related Questions
How to Instantiate only once? 2 Answers
Instantiate Random Object at Random Position 1 Answer
Making more than one item appear on a chess board 2 Answers
Mulitple spawn points with random seed. 3 Answers
Puzzle + Grid Instantiate - Random 2 Answers