- Home /
Pretty complicated
So, hi there! :), I am trying to have a car game, which everyone can buy cars, (and I save that data at playerprefs), So I have 9 trails for the cars in my game, I am trying to have a code which when you press a button, the car&the trail of the car will show up, and the next button next to it, will be interactable & to save that data so when people restart the game, they will still have the car&trail open and wont be needing to press the button again, here's my code: , please tell me what I'm doing wrong / help me to fix it Thanks!: ) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameManager : MonoBehaviour { public Button[] TrailLevel; public GameObject[] Cars,Trails; public Text text; public int CurrentCarToSpawn=0; private void Start() {
}
private void FixedUpdate()
{
UpdateCar();
}
public void InstantiateCar()
{
TrailLevel[CurrentCarToSpawn].gameObject.active = false;
MineLevel[CurrentCarToSpawn+1].interactable = true;
PlayerPrefs.SetInt("TrailCountA", PlayerPrefs.GetInt("TrailCountA") + 1);
PlayerPrefs.Save();
CurrentCarToSpawn++;
UpdateCar();
}
void UpdateCar()
{
int TrailCountA= PlayerPrefs.GetInt("TrailCountA", 1);
for (int i = 0; i < TrailLevel.Length; i++)
{
if (i + 1 > TrailCountA)
{
TrailLevel[i].interactable = false;
}
if (TrailLevel[i].interactable)
{
Trains[CurrentCarToSpawn].gameObject.active = true;
Mines[CurrentCarToSpawn].gameObject.active = true;
}
}
text.text = PlayerPrefs.GetInt("TrailCountA").ToString();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612175821im_/https://answers.unity.com/themes/thub/images/avi.jpg)