- Home /
PlayerPrefs to Unlock Object question
I'm relatively new to Unity and have been building an iPhone app for quite some time. I want to have in app purchases for certain gameobjects and I'm wondering how I would go about doing that. Everything I read is I need to use PlayerPrefs but I can't find a good tutorial. I need it to work with Prime31's Storekit plugin which I have up and running. I can easily make purchases with this great plugin but my noobish nature isn't allowing me to figure out how to assign this to an object to lock and unlock it once purchase is successful.
Thanks to anyone that can help me or point me to a good tutorial which I can't find for the life of me!
Answer by whydoidoit · Jun 22, 2012 at 10:04 AM
How about something like this:
On purchase:
PlayerPrefs.SetInt("Bought_" +itemName, 1);
To check for purchase:
if(PlayerPrefs.HasKey("Bought_" + itemName)) {
//Item was purchased
}
I don't mean to be dense but where would I put this code? Sorry. Still learning!
So put the On Purchase code where you buy the item and the other code anywhere where you want to know if the item has been purchased...
So the only code I have setup right now that pertains to this issue is the in app purchase plugin which tests fine. Let's say I have a cube that I want to unlock in a particular scene. Do I place the on purchase on the cube and the item was purchased on the scene? Thanks for your help. I appreciate it.
Yes - so i your store unlock you put that On purchase code then if you you want the item to only appear if it was purchased add a script with this in it to the item in your scene:
var itemName : String;
void Awake() {
if(!PlayerPrefs.Has$$anonymous$$ey("Bought_" + itemName)) {
Destroy(gameObject);
}
}
You can put that same script on each item that can be bought and set the itemName in the inspector to match the name of the item when you buy it.
You would have to reload your scene after a purchase with that code - if you want something cleverer than that I can give it a go...
Your answer
Follow this Question
Related Questions
How to bind Prime31 ios storekit plugin with GUIText elements? 1 Answer
Problem with Prime31 StoreKit plugin 0 Answers
Storekit Product details stopped showing ? 0 Answers
Prime31 StoreKit Question 0 Answers