- Home /
Question by
Paul 6 · Feb 11, 2011 at 05:47 PM ·
instantiateprefabplayerprefssavereload
Instantiate a prefab just one time
How I Instantiate a prefab just one time if I restart a level. S0mething like a coin in a platformer game, you collect it and when you enter the level another time the coin isn't there, it hasn't been instantiated. How can I archieve this?
Comment
Best Answer
Answer by Molix · Feb 11, 2011 at 07:10 PM
Assuming you want it to work between game sessions, then you need to persist the fact that the player has picked it up. There are several ways, depending on the type of game, and how many you need to save. A simple solution would be to use PlayerPrefs. When the scene loads, you just need a component that does something like:
void Start()
{
if( PlayerPrefs.GetInt("ItemPickedUp") == 0 )
{
// instantiate it here
}
}
And when they pick it up, in whatever "Pickup" function you have, add:
// store that they picked it up so we don't respawn it
PlayerPrefs.SetInt("ItemPickedUp", 1 );