How to make a button that unlocks a reward?

Hello everyone. For a project for my university, I have to create a small game that is a quiz. I have zero experience in Unity and coding in general. The quiz has separate chapters and upon completing each chapter, I want something unlocked in a different scene. I think this has to be done with PlayerPrefs, but I am not sure how to actually get this working. I do know that the reward should be active in the scene throughout the entire game once it is activated. What I essentially want is that upon clicking the last UI button of the chapter a reward image appears in the reward screen. To me it would seem the most easy to do this in a OnClick event, but I have tried for several hours (yes, hours…) and I just cannot get it working. Any tips would be greatly appreciated! I am working in C#.

The button, what unlocks the reward, must call some your script wich saves 1 to playerprefs with particular name. Lets’ pretend we have “Gold” “Silver” and “Bronze” rewards. To unlock “Silver”, set “Silver_unlocked” to 1 and save playerprefs.
Everywhere this reward might be available check if key “Silver_unlocked” exists in prefs and equals to one. If not, disable game object with reward. If yes, enable that object.

1 Like

Hi.
To make that reward image appear you need a function that -duh- makes it appear. That image could be initially inactive, and in that function, you activate it.

public void ActivateReward() { m_rewardImage.SetActive(true); }

m_rewardImage would be a (public) GameObject. You fill that object in the inspector, dragging and dropping the reward image game object to that slot in the inspector.

Call that function from the OnClick() of the button. You need to select the ActivateReward function from the script where you can find that function from the GameObject that you attach to the OnClick event.

2 Likes

Hello, thank you! How would I make it so that unlock persists through all scenes? Once I move between scenes it now resets the scene. The reward image is in the scene RewardScreen and the chapters have individual scenes in this case.

I’d go for only one scene. Only one canvas to hold all the different panels/quiz and then I would SetActive panels 1 at a time (deactivating previous quiz). Way simpler approach than having multiple scenes.

If you still want 1 scene per quiz one way of doing it would be to store information in a file/PlayerPrefs and then checking that data on Startup each Scene. You could also keep adding scenes on LoadScene (using LoadSceneMode.Additive as second parameter to the function) so that you wouldn’t delete GameObjects from previous scenes.

Still, I suggest going with the first approach (only 1 scene) to keep it cleaner.

1 Like

PlayerPrefs preserved across all scenes and inbetween app runs. So if you set a key, you will get the same value on same device in every scene and in every game session, until you delete or modify that key.

1 Like