Unity Store System

0

Hello. I need a way to create a car shop in a game in Unity. I started to create the store, but I do not find a way to show the car bought in another scene, that is, the game know that you bought that car.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CarShop : MonoBehaviour {

public string carroSelected;
public Text velMaxima;
public Text Acele;
public Text cambio;
public Text Motor;
public Text preco;
public Text nome;
public Text carroaviso;
public GameObject confirmar;
public GameObject semDinheiro;
public GameObject efetuada;
//public GameObject capContorno;
//public GameObject copContorno;
//public GameObject viaContorno;
//public GameObject nviaContorno;
public float dinheiro;
public float precoCarro;

// Use this for initialization
void Start () {
    velMaxima.text = "0 km/H";
    Acele.text = "0s";
    cambio.text = "0 Marchas";
    Motor.text = "NULL";
    dinheiro = PlayerPrefs.GetFloat("dinheiro");
    nome.text = "Selecione um carro";
    preco.text = "";
    carroSelected = "";
}

// Update is called once per frame
void Update () {

    precoCarro = PlayerPrefs.GetFloat("precoCarro");
    if(dinheiro >= precoCarro)
    {
        preco.color = Color.green;
    }
    if(dinheiro < precoCarro)
    {
        preco.color = Color.red;
    }
    PlayerPrefs.SetString("CarroAtual", carroSelected);
    PlayerPrefs.SetFloat("dinheiro", dinheiro);

}

public void sim()
{
    Bought();
}

public void nao()
{
    confirmar.SetActive(false);
}

public void Buy()
{
    if(dinheiro >= precoCarro && carroSelected != "")
    {
    precoCarro = PlayerPrefs.GetFloat("precoCarro");
    confirmar.SetActive(true);
    carroaviso.text = nome.text;
    }
    if(dinheiro < precoCarro && carroSelected != "")
    {
        semDinheiro.SetActive(true);
        Invoke("semD", 3f);
    }
}

void semD ()
{
    semDinheiro.SetActive(false);
}

void Bought()
{
    //Carro Comprado
    //-----------------------------------------------------------
    //Capital
    if(carroSelected == "Wolk Capital")
    {
        PlayerPrefs.SetString("ultComprado", "Wolk Capital");
    }
    //Copa
    if(carroSelected == "Wolk Copa")
    {
        PlayerPrefs.SetString("ultComprado", "Wolk Capital");
    }
    //Viagem
    if(carroSelected == "Wolk Viagem")
    {
        PlayerPrefs.SetString("ultComprado", "Wolk Capital");
    }
    //Novo Viagem
    if(carroSelected == "Wolk Novo Viagem")
    {
        PlayerPrefs.SetString("ultComprado", "Wolk Capital");
    }
    //===========================================================

    PlayerPrefs.SetInt("carrosSLOT", PlayerPrefs.GetInt("carrosSLOT") + 1);
    if(dinheiro >= precoCarro)
    {
        spend(precoCarro);
        confirmar.SetActive(false);
    }

}

void spend(float amount)
{
    dinheiro = PlayerPrefs.GetFloat("dinheiro");
    dinheiro -= amount;
    efetuada.SetActive(true);
    Invoke("efD", 3f);
}

void efD () 
{
    efetuada.SetActive(false);
}

public void capitalSel () {
    PlayerPrefs.SetFloat("precoCarro", 3000);
    precoCarro = PlayerPrefs.GetFloat("precoCarro");
    carroSelected = "Wolk Capital";
    nome.text = carroSelected;
    velMaxima.text = "110 km/h";
    Acele.text = "21.90s";
    cambio.text = "4 Marchas";
    Motor.text = "Boxxer";
    preco.text = "3000R$";
    //capContorno.SetActive(true);
    //copContorno.SetActive(false);
    //viaContorno.SetActive(false);
    //nviaContorno.SetActive(false);
}

public void copaSel () {
    PlayerPrefs.SetFloat("precoCarro", 8000);
    precoCarro = PlayerPrefs.GetFloat("precoCarro");
    carroSelected = "Wolk Copa";
    nome.text = carroSelected;
    velMaxima.text = "170 km/h";
    Acele.text = "10.3s";
    cambio.text = "5 Marchas";
    Motor.text = "AP (Alta Performance)";
    preco.text = "8000R$";
    //capContorno.SetActive(false);
    //copContorno.SetActive(true);
    //viaContorno.SetActive(false);
    //nviaContorno.SetActive(false);
}

public void viagemSel () {
    PlayerPrefs.SetFloat("precoCarro", 11000);
    precoCarro = PlayerPrefs.GetFloat("precoCarro");
    carroSelected = "Wolk Viagem Super";
    nome.text = carroSelected;
    velMaxima.text = "170 km/h";
    Acele.text = "11.5s";
    cambio.text = "5 Marchas";
    Motor.text = "AP (Alta Performance)";
    preco.text = "11000R$";
    //capContorno.SetActive(false);
    //copContorno.SetActive(false);
    //viaContorno.SetActive(true);
    //nviaContorno.SetActive(false);
}

public void nViagemSel () {
    PlayerPrefs.SetFloat("precoCarro", 40000);
    precoCarro = PlayerPrefs.GetFloat("precoCarro");
    carroSelected = "Wolk Novo Viagem";
    nome.text = carroSelected;
    velMaxima.text = "190 km/h";
    Acele.text = "10s";
    cambio.text = "5 Marchas";
    Motor.text = "Power EA";
    preco.text = "40000R$";
    //capContorno.SetActive(false);
    //copContorno.SetActive(false);
    //viaContorno.SetActive(false);
    //nviaContorno.SetActive(true);
}

}
    
asked by anonymous 15.02.2018 / 05:18

1 answer

0

There are several ways to perserve data, here are some suggestions:

PlayerPrefs Class

int valor = 1;

// para gravar
PlayerPrefs.SetInt("valor_exemplo", valor);

// para obter o valor
valor =  PlayerPrefs.GetInt("valor_exemplo");

PlayerPrefs Documentation

Another solution:

Use the DontDestroyOnLoad function to perserve an object and all its components for the next scene

Ex: DontDestroyOnLoad(objecto_loja);

Using a file

You can also use a file to write and read values, and you will be able to keep values after the program closes.

See this article .

    
15.02.2018 / 11:03