- Home /
destroy pickups after you collect them
Hi,
I was wondering how to destroy a certain amount of pickups when the game is started, based on how mant pickups I have collected.
For Exsample:
Lets say I have a scene with 100 pickups and I collect 50 of them, save and then exit the game. Then when start the game and press 'L' to load only 50 pickups should appear.
I already have a save system that stores the amount of pickups collected to a text file when I press the 'S' key and loads that information when I press the 'L' key.
either java script or c sharp code is fine.
thanks.
Answer by spinaljack · Aug 29, 2010 at 01:20 AM
Simple method would be to use a for loop to destroy a certain number of objects with the pickup tag. Or you could instantiate the pickups at run time unless you want them in a very specific place.
e.g.
var pickups : GameObject[] = GameObject.FindObjectsWithTag("pickups"); var numberToDestory : int = 10;
for(i=0;i<numberToDestroy;i++){ Destroy(pickups[i]); }
It says $$anonymous$$ Identifier : numberToDestroy when I typed the code.
Answer by tylo · Aug 29, 2010 at 03:57 AM
If you wanted to only spawn certain pick-ups on re-load, I would save the locations of those that are left. That way, when you re-load your scene, you spawn only the ones that the user didn't pick up.
To word it another way...
There is no need to Destroy anything in this case. Just save what the user hasn't collected, and Instantiate them in the same spots on re-load.
You say you already have a save system that is storing the number of collected items to a text file.
Ins$$anonymous$$d of saving the number, store the locations of each non-collected object when the player saves the game. There are many different methods for saving a game's state.
I feel this should be enough information to solve your problem.
If you have further specific Unity scripting questions, you may want to follow some tutorials or create new questions.
Your answer
Follow this Question
Related Questions
How to have score when an object is destroyed 1 Answer
PickUp (Destroy) gameObject one by one 3 Answers
I want to use the current scale x of my object to help calculate my score. . . 1 Answer
high scores scene 2 Answers
saving highscores 1 Answer