I made a shop system to my 2D platform game but when i close the shop my platform game restarts from beginning how can i fix that? There is some codes i used about respawning ingame
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public GameObject GOPanel;
AudioSource DieSound;
// Start is called before the first frame update
void Start()
{
Time.timeScale = 1;
DieSound = GetComponent<AudioSource>();
}
public void GameOver()
{
DieSound.Play();
Time.timeScale = 0;
GOPanel.SetActive(true);
}
public void RestartGame()
{
SceneManager.LoadScene("Demo");
}
}
____________________________________________________________________________________
private void OnCollisionEnter2D(Collision2D collision)
{
isJumping = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "upstacle")
{
gameManager.GameOver();
}
}
First of all, whenever you paste code snippets always use code tags!
I’m not sure why you showed us that script, showing the shop script would be a better idea
When you reload a scene it starts out all afresh. You need to either save what its state was, or enough of what its state was to load it, then modify it in the first frame before the game plays.
This is identical to what happens with load/save games.
Don’t use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.
When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.
Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.
If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:
Sorry im very new to coding and unity , there is shop and exip shop script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ShopControl : MonoBehaviour
{
int moneyAmount;
int isHatSold;
public Text moneyAmountText;
public Text hatPrice;
public Button buyButton;
// Start is called before the first frame update
void Start()
{
moneyAmount = PlayerPrefs.GetInt ("MoneyAmount");
}
// Update is called once per frame
void Update()
{
moneyAmountText.text = "Money: " + moneyAmount.ToString() + "$";
isHatSold = PlayerPrefs.GetInt("IsHatSold");
if (moneyAmount >= 100 && isHatSold == 0)
buyButton.interactable = true;
else
buyButton.interactable = false;
}
public void buyHat()
{
moneyAmount -= 100;
PlayerPrefs.SetInt ("isHatSold", 1);
hatPrice.text = "Sold!";
buyButton.gameObject.SetActive (false);
}
public void exitShop()
{
PlayerPrefs.SetInt("MoneyAmount", moneyAmount);
SceneManager.LoadScene("Demo");
}
public void resetPlayerPrefs()
{
moneyAmount = 0;
buyButton.gameObject.SetActive(true);
hatPrice.text = "100 Coin";
PlayerPrefs.DeleteAll();
}
}
If the Demo scene does not take measures to store its previous state, it will be loaded afresh the way it was the first time it was loaded. See the rest of what I wrote above as to ways to address it.
Another way is to just turn off all the main scene objects and turn on your store, when when you leave the store reverse that process.