- Home /
Making a function only happen once in the entire game.
Hello! I figure the best way to do this is simply to define things and save it in a file if it is enabled or not, but I was wondering if there is a better way to do things?
Heres my scenario:
I have an object which gives the player an Item (Let us call it ItemA) and I want it to only give it once, easy enough, only trigger it once then disable the script component that gave the item.
But when restarting the game, the trigger/object/script would obviously be enabled again.
I was just wondering, is there a good way to keep track of this or should I simply just put the "use" information in a file and call it to check whether to disable or enable an object?
Answer by ShadyProductions · Mar 08, 2019 at 08:45 PM
You can use PlayerPrefs to save data.
PlayerPrefs.SetString("itemGiven", "true");
you can then use PlayerPrefs.GetString("itemGiven") == "true"
to check if it has been set already.
I suppose files were indeed the way then. Though I do save things via Json ins$$anonymous$$d of playerprefs unless they are control related and... well, preferences.
Dont forget about the encode this:D Because player can edit this value in easy way ;)
Haha I am not too worried about it, everything is turned into 0s and 1s in an array, so I don't think they'll be able to figure out which is what
Your answer
Follow this Question
Related Questions
How to use PlayerPrefs to stop an item from reappearing when you return to the level 2 Answers
How to make sure score can't be increased by picking up same item multipe times--new idea 2 Answers
Save Scores In Scene 4 Answers
How to script a Save/Load option on IPhone? 1 Answer
Saving Game Problem 1 Answer