Hi, I programmed a little mobile game where you play the same level over and over again to farm and sometimes you get an item when you finish the level. After completing the level the item should be activated in the inventory, but as soon as the scene is restarted, everything is back to the beginning.
This code is when you press the button to get the item.
Thanks in advance,
-JONAS Philippe
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class reward : MonoBehaviour
{
public int itemReached;
public GameObject Item;
// Start is called before the first frame update
void Start()
{
if (PlayerPrefs.GetInt("ItemON", 0) == 1)
{
Item.SetActive(true);
}
/*
if (itemReached == 1)
{
Item.SetActive(true);
}
*/
}
// Update is called once per frame
void Update()
{
itemReached = PlayerPrefs.GetInt("ItemON");
}
public void ButtonPress()
{
Save();
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void Save()
{
PlayerPrefs.SetInt("ItemON", 1);
PlayerPrefs.Save();
}
}