Unity Store System

Asked

Viewed 298 times

2

Hello. I need a way to create a car shop in a Unity game. I started to create the store, but I can’t find a way to show the car purchased in another scene, ie 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);
}

}
  • One way that you can work with this is by creating a script in a separate Gameobject and in it you use the Dontdestroyonload(this.gameobject) method passing as parameter this gameobject. In this script you will also have a variable of type Gameobject that will have the car chosen in the purchase and then in the start you will only recover this value and instantiate the car.

1 answer

1

There are several ways to preserve 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");

Documentation Playerprefs

Another solution:

Use the function Dontdestroyonload to preserve an object and all its components for the sequence.

Ex: DontDestroyOnLoad(objecto_loja);

Use a file

You can also use a file to save and read values so you can keep values after program shutdown.

See this article.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.