Please help

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*.interactable = false;*
    • }*
      - if (TrailLevel*.interactable)*
      - - {*
      - - Trains[CurrentCarToSpawn].gameObject.active = true;*
      - - Mines[CurrentCarToSpawn].gameObject.active = true;*
      - - }*
      - - }*
      - - text.text = PlayerPrefs.GetInt(“TrailCountA”).ToString();*
      - - }*
      - [/i][/i]*

Don’t call UpdateCar in FixedUpdate. You should only have to access PlayerPrefs when the game loads and you need to get the last saved data or when a new trail/car is selected and you need to save that data out.

Otherwise, you should just use the data once you get it. And a suggestion. Instead of all the GetInt calls, just call the value once at the start and assign it to a private variable. Then if a car for example is changed, update that variable and resave it out to your playerpref.

Also a forum tip. Use code tags. Also a better forum title will help. (“Please Help” isn’t likely to get you help) You’re more likely to get help. :slight_smile: